gpt4 book ai didi

Javascript 引用字符串到数组

转载 作者:行者123 更新时间:2023-11-30 08:21:27 25 4
gpt4 key购买 nike

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

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

有什么帮助吗?

最佳答案

此答案假定您引用的国家/地区字符串实际上定义为:

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

关于Javascript 引用字符串到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52960947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com