Javascript将字符串引用到数组

时间:2018-10-24 04:06:30

标签: javascript regex

我正在尝试将这些字符串转换为数组的元素:

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));

有帮助吗?

2 个答案:

答案 0 :(得分:3)

此答案假设您所引用的国家/地区字符串实际上是这样定义的:

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);

答案 1 :(得分:1)

一旦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));

我所做的只是使用单引号将列表包装起来,然后转义内容内部的任何单引号(例如,“老挝人民民主共和国”必须成为“老挝人民民主共和国”。否则,似乎工作正常。