Я пытаюсь преобразовать эти строки в элементы массива:

strings = "Antigua and Barbuda"
                    "Argentina"
                    "Armenia"
                    "Aruba"
                    "Australia"
                    "Austria"
                    "Azerbaijan"
                    "Bahamas"
                    "Bahrain"
                    "Bangladesh"
                    "Barbados"
                    "Belarus"
                    "Belize"
                    "Benin"
                    "Bermuda"
                    "Bhutan"
                    "Bolivia"
                    "Bosnia and Herzegovina"
                    "Botswana"
                    "Bouvet Island"
                    "Brazil"
                    "British Indian Ocean Territory"
                    "Brunei Darussalam"
                    "Bulgaria"
                    "Burkina Faso"
                    "Burundi"
                    "Cambodia"
                    "Cameroon"
                    "Canada"
                    "Cape Verde"
                    "Cayman Islands"
                    "Central African Republic"
                    "Chad"
                    "Chile"
                    "China"
                    "Christmas Island"
                    "Cocos (Keeling) Islands"
                    "Colombia"
                    "Comoros"
                    "Congo, The Democratic Republic of The"
                    "Cook Islands"
                    "Croatia"
                    "Cuba"
                    "Cyprus"
                    "Denmark"
                    "Djibouti"
                    "Dominica"
                    "Dominican Republic"
                    "Ecuador"
                    "Egypt"
                    "El Salvador"
                    "Equatorial Guinea"
                    "Eritrea"
                    "Estonia"
                    "Ethiopia"
                    "Falkland Islands (Malvinas)"
                    "Faroe Islands"
                    "Fiji"
                    "Finland"
                    "French Guiana"
                    "French Polynesia"
                    "French Southern Territories"
                    "Gabon"
                    "Gambia"
                    "Georgia"
                    "Ghana"
                    "Gibraltar"
                    "Greece"
                    "Greenland"
                    "Grenada"
                    "Guadeloupe"
                    "Guam"
                    "Guinea"
                    "Guinea-bissau"
                    "Guyana"
                    "Haiti"
                    "Heard Island and Mcdonald Islands"
                    "Holy See (Vatican City State)"
                    "Honduras"
                    "Hong Kong"
                    "Hungary"
                    "Iceland"
                    "India"
                    "Indonesia"
                    "Iran, Islamic Republic of"
                    "Iraq"
                    "Ireland"
                    "Jamaica"
                    "Japan"
                    "Jordan"
                    "Kazakhstan"
                    "Kenya"
                    "Kiribati"
                    "Korea, Democratic People's Republic of"
                    "Korea, Republic of"
                    "Kuwait"
                    "Kyrgyzstan"
                    "Lao People's Democratic Republic"
                    "Latvia"
                    "Lesotho"
                    "Liberia"
                    "Libyan Arab Jamahiriya"
                    "Liechtenstein"
                    "Lithuania"
                    "Luxembourg"
                    "Macao"
                    "Macedonia, The Former Yugoslav Republic of"
                    "Madagascar"
                    "Malawi"
                    "Malaysia"
                    "Maldives"
                    "Mali"
                    "Malta"
                    "Marshall Islands"
                    "Martinique"
                    "Mauritania"
                    "Mauritius"
                    "Mayotte"
                    "Micronesia, Federated States of"
                    "Moldova, Republic of"
                    "Monaco"
                    "Mongolia"
                    "Montserrat"
                    "Mozambique"
                    "Myanmar"
                    "Namibia"
                    "Nauru"
                    "Nepal"
                    "Netherlands Antilles"
                    "New Caledonia"
                    "New Zealand"
                    "Nicaragua"
                    "Niger"
                    "Nigeria"
                    "Niue"
                    "Norfolk Island"
                    "Northern Mariana Islands"
                    "Norway"
                    "Oman"
                    "Pakistan"
                    "Palau"
                    "Palestinian Territory"
                    "Papua New Guinea"
                    "Paraguay"
                    "Peru"
                    "Philippines"
                    "Pitcairn"
                    "Poland"
                    "Portugal"
                    "Puerto Rico"
                    "Qatar"
                    "Reunion"
                    "Romania"
                    "Russian Federation"
                    "Rwanda"
                    "Saint Helena"
                    "Saint Kitts and Nevis"
                    "Saint Lucia"
                    "Saint Pierre and Miquelon"
                    "Saint Vincent and The Grenadines"
                    "Samoa"
                    "San Marino"
                    "Sao Tome and Principe"
                    "Saudi Arabia"
                    "Senegal"
                    "Serbia and Montenegro"
                    "Seychelles"
                    "Sierra Leone"
                    "Singapore"
                    "Slovakia"
                    "Slovenia"
                    "Solomon Islands"
                    "Somalia"
                    "South Africa"
                    "South Georgia and The South Sandwich Islands"
                    "Spain"
                    "Sri Lanka"
                    "Sudan"
                    "Suriname"
                    "Svalbard and Jan Mayen"
                    "Swaziland"
                    "Syrian Arab Republic"
                    "Taiwan, Province of China"
                    "Tajikistan"
                    "Tanzania, United Republic of"
                    "Thailand"
                    "Timor-leste"
                    "Togo"
                    "Tokelau"
                    "Tonga"
                    "Trinidad and Tobago"
                    "Turkey"
                    "Turkmenistan"
                    "Turks and Caicos Islands"
                    "Tuvalu"
                    "Uganda"
                    "Ukraine"
                    "United Kingdom"
                    "United States Minor Outlying Islands"
                    "Uruguay"
                    "Uzbekistan"
                    "Vanuatu"
                    "Venezuela"
                    "Viet Nam"
                    "Virgin Islands, British"
                    "Virgin Islands, U.S."
                    "Wallis and Futuna"
                    "Western Sahara"
                    "Yemen"
                    "Zambia"
                    "Zimbabwe";

Я пробовал использовать следующую функцию, но она не работает:

function extractAllText(str){
  const re = /"(.*?)"/g;
  const result = [];
  let current;
  while (current = re.exec(str)) {
    result.push(current.pop());
  }
  return result;
}

console.log(extractAllText(strings));

Любая помощь?

1
user4675957 24 Окт 2018 в 07:06

2 ответа

Лучший ответ

Этот ответ предполагает, что ваша строка цитируемых стран на самом деле определяется следующим образом:

var input = "\"Antigua and Barbuda\" \"Argentina\" \"Armenia\"";

То есть у вас есть правильные ограничивающие кавычки вокруг вашей строки стран. Мы можем попробовать разделить на:

" "

После удаления первой и конечной двойных кавычек из строки. Это оставит нас с множеством стран без котировок.

var input = "\"Antigua and Barbuda\" \"Argentina\" \"Armenia\"";
input = input.replace(/^"(.*)"$/, "$1");
var countries = input.split("\" \"");
console.log(countries);
4
Tim Biegeleisen 24 Окт 2018 в 04:11

Как только ваша переменная strings правильно сформирована со строкой-оболочкой, ваш написанный код будет работать нормально.

const strings = '"Antigua and Barbuda" "Argentina" "Armenia" "Aruba" "Australia" "Austria" "Azerbaijan" "Bahamas" "Bahrain" "Bangladesh" "Barbados" "Belarus" "Belize" "Benin" "Bermuda" "Bhutan" "Bolivia" "Bosnia and Herzegovina" "Botswana" "Bouvet Island" "Brazil" "British Indian Ocean Territory" "Brunei Darussalam" "Bulgaria" "Burkina Faso" "Burundi" "Cambodia" "Cameroon" "Canada" "Cape Verde" "Cayman Islands" "Central African Republic" "Chad" "Chile" "China" "Christmas Island" "Cocos (Keeling) Islands" "Colombia" "Comoros" "Congo, The Democratic Republic of The" "Cook Islands" "Croatia" "Cuba" "Cyprus" "Denmark" "Djibouti" "Dominica" "Dominican Republic" "Ecuador" "Egypt" "El Salvador" "Equatorial Guinea" "Eritrea" "Estonia" "Ethiopia" "Falkland Islands (Malvinas)" "Faroe Islands" "Fiji" "Finland" "French Guiana" "French Polynesia" "French Southern Territories" "Gabon" "Gambia" "Georgia" "Ghana" "Gibraltar" "Greece" "Greenland" "Grenada" "Guadeloupe" "Guam" "Guinea" "Guinea-bissau" "Guyana" "Haiti" "Heard Island and Mcdonald Islands" "Holy See (Vatican City State)" "Honduras" "Hong Kong" "Hungary" "Iceland" "India" "Indonesia" "Iran, Islamic Republic of" "Iraq" "Ireland" "Jamaica" "Japan" "Jordan" "Kazakhstan" "Kenya" "Kiribati" "Korea, Democratic People\'s Republic of" "Korea, Republic of" "Kuwait" "Kyrgyzstan" "Lao People\'s Democratic Republic" "Latvia" "Lesotho" "Liberia" "Libyan Arab Jamahiriya" "Liechtenstein" "Lithuania" "Luxembourg" "Macao" "Macedonia, The Former Yugoslav Republic of" "Madagascar" "Malawi" "Malaysia" "Maldives" "Mali" "Malta" "Marshall Islands" "Martinique" "Mauritania" "Mauritius" "Mayotte" "Micronesia, Federated States of" "Moldova, Republic of" "Monaco" "Mongolia" "Montserrat" "Mozambique" "Myanmar" "Namibia" "Nauru" "Nepal" "Netherlands Antilles" "New Caledonia" "New Zealand" "Nicaragua" "Niger" "Nigeria" "Niue" "Norfolk Island" "Northern Mariana Islands" "Norway" "Oman" "Pakistan" "Palau" "Palestinian Territory" "Papua New Guinea" "Paraguay" "Peru" "Philippines" "Pitcairn" "Poland" "Portugal" "Puerto Rico" "Qatar" "Reunion" "Romania" "Russian Federation" "Rwanda" "Saint Helena" "Saint Kitts and Nevis" "Saint Lucia" "Saint Pierre and Miquelon" "Saint Vincent and The Grenadines" "Samoa" "San Marino" "Sao Tome and Principe" "Saudi Arabia" "Senegal" "Serbia and Montenegro" "Seychelles" "Sierra Leone" "Singapore" "Slovakia" "Slovenia" "Solomon Islands" "Somalia" "South Africa" "South Georgia and The South Sandwich Islands" "Spain" "Sri Lanka" "Sudan" "Suriname" "Svalbard and Jan Mayen" "Swaziland" "Syrian Arab Republic" "Taiwan, Province of China" "Tajikistan" "Tanzania, United Republic of" "Thailand" "Timor-leste" "Togo" "Tokelau" "Tonga" "Trinidad and Tobago" "Turkey" "Turkmenistan" "Turks and Caicos Islands" "Tuvalu" "Uganda" "Ukraine" "United Kingdom" "United States Minor Outlying Islands" "Uruguay" "Uzbekistan" "Vanuatu" "Venezuela" "Viet Nam" "Virgin Islands, British" "Virgin Islands, U.S." "Wallis and Futuna" "Western Sahara" "Yemen" "Zambia" "Zimbabwe"';

function extractAllText(str){
  const re = /"(.*?)"/g;
  const result = [];
  let current;
  while (current = re.exec(str)) {
    result.push(current.pop());
  }
  return result;
}

console.log(extractAllText(strings));

Все, что я сделал, это заключил список в одинарные кавычки, а затем избавился от одинарных кавычек внутри содержимого (например, «Лаосская Народно-Демократическая Республика» должна стать «Лаосской Народно-Демократической Республикой». В противном случае, похоже, все работает нормально ,

1
Alexander Nied 24 Окт 2018 в 04:15
52960947