Skip to content
Advertisement

Regex for Extracting the Country Name

What regular expression would extract the country name when used with any of the lines below?

I’ve got a dropdown with all of these as choices and I’m trying to extract the country only, but I’m failing miserably since JavaScript doesn’t seem to support lookbehinds and I have no idea how to exclude the emoji part otherwise. (Not to mention that special characters such as that ร… in ร…land Islands don’t make it any easier.)

Thanks!

๐Ÿ‡ฆ๐Ÿ‡ซ Afghanistan +93
๐Ÿ‡ฆ๐Ÿ‡ฝ ร…land Islands +358
๐Ÿ‡ฆ๐Ÿ‡ฑ Albania +355
๐Ÿ‡ฉ๐Ÿ‡ฟ Algeria +213
๐Ÿ‡ฆ๐Ÿ‡ธ American Samoa +1684
๐Ÿ‡ฆ๐Ÿ‡ฉ Andorra +376
๐Ÿ‡ฆ๐Ÿ‡ด Angola +244
๐Ÿ‡ฆ๐Ÿ‡ฎ Anguilla +1264
๐Ÿ‡ฆ๐Ÿ‡ฌ Antigua & Barbuda +1268
๐Ÿ‡ฆ๐Ÿ‡ท Argentina +54
๐Ÿ‡ฆ๐Ÿ‡ฒ Armenia +374
๐Ÿ‡ฆ๐Ÿ‡ผ Aruba +297
๐Ÿ‡ฆ๐Ÿ‡บ Australia +61
๐Ÿ‡ฆ๐Ÿ‡น Austria +43
๐Ÿ‡ฆ๐Ÿ‡ฟ Azerbaijan +994
๐Ÿ‡ง๐Ÿ‡ธ Bahamas +1242
๐Ÿ‡ง๐Ÿ‡ญ Bahrain +973
๐Ÿ‡ง๐Ÿ‡ฉ Bangladesh +880
๐Ÿ‡ง๐Ÿ‡ง Barbados +1246
๐Ÿ‡ง๐Ÿ‡พ Belarus +375
๐Ÿ‡ง๐Ÿ‡ช Belgium +32
๐Ÿ‡ง๐Ÿ‡ฟ Belize +501
๐Ÿ‡ง๐Ÿ‡ฏ Benin +229
๐Ÿ‡ง๐Ÿ‡ฒ Bermuda +1441
๐Ÿ‡ง๐Ÿ‡น Bhutan +975
๐Ÿ‡ง๐Ÿ‡ด Bolivia +591
๐Ÿ‡ง๐Ÿ‡ฆ Bosnia & Herzegovina +387
๐Ÿ‡ง๐Ÿ‡ผ Botswana +267
๐Ÿ‡ง๐Ÿ‡ท Brazil +55
๐Ÿ‡ฎ๐Ÿ‡ด British Indian Ocean Territory +246
๐Ÿ‡ป๐Ÿ‡ฌ British Virgin Islands +1284
๐Ÿ‡ง๐Ÿ‡ณ Brunei +673
๐Ÿ‡ง๐Ÿ‡ฌ Bulgaria +359
๐Ÿ‡ง๐Ÿ‡ซ Burkina Faso +226
๐Ÿ‡ง๐Ÿ‡ฎ Burundi +257
๐Ÿ‡ฐ๐Ÿ‡ญ Cambodia +855
๐Ÿ‡จ๐Ÿ‡ฒ Cameroon +237
๐Ÿ‡จ๐Ÿ‡ฆ Canada +1
๐Ÿ‡จ๐Ÿ‡ป Cape Verde +238
๐Ÿ‡ณ๐Ÿ‡ฑ Carribbean Netherlands +599
๐Ÿ‡ฐ๐Ÿ‡พ Cayman Islands +1345
๐Ÿ‡จ๐Ÿ‡ซ Central African Republic +236
๐Ÿ‡น๐Ÿ‡ฉ Chad +235
๐Ÿ‡จ๐Ÿ‡ฑ Chile +56
๐Ÿ‡จ๐Ÿ‡ณ China +86
๐Ÿ‡จ๐Ÿ‡ฝ Christmas Islands +61
๐Ÿ‡จ๐Ÿ‡จ Cocos Islands +61
๐Ÿ‡จ๐Ÿ‡ด Colombia +57
๐Ÿ‡ฐ๐Ÿ‡ฒ Comoros +269
๐Ÿ‡จ๐Ÿ‡ฉ Congo-Kinshasa +243
๐Ÿ‡จ๐Ÿ‡ฌ Congo-Brazzaville +242
๐Ÿ‡จ๐Ÿ‡ฐ Cook Islands +682
๐Ÿ‡จ๐Ÿ‡ท Costa Rica +506
๐Ÿ‡ญ๐Ÿ‡ท Croatia +385
๐Ÿ‡จ๐Ÿ‡บ Cuba +53
๐Ÿ‡จ๐Ÿ‡ผ Curaรงao +599
๐Ÿ‡จ๐Ÿ‡พ Cyprus +357
๐Ÿ‡จ๐Ÿ‡ฟ Czechia +420
๐Ÿ‡ฉ๐Ÿ‡ฐ Denmark +45
๐Ÿ‡ฉ๐Ÿ‡ฏ Djibouti +253
๐Ÿ‡ฉ๐Ÿ‡ฒ Dominica +1767
๐Ÿ‡ฉ๐Ÿ‡ด Dominican Republic +1
๐Ÿ‡ช๐Ÿ‡จ Ecuador +593
๐Ÿ‡ช๐Ÿ‡ฌ Egypt +20
๐Ÿ‡ธ๐Ÿ‡ป El Salvador +503
๐Ÿ‡ฌ๐Ÿ‡ถ Equatorial Guinea +240
๐Ÿ‡ช๐Ÿ‡ท Eritrea +291
๐Ÿ‡ช๐Ÿ‡ช Estonia +372
๐Ÿ‡ช๐Ÿ‡น Ethiopia +251
๐Ÿ‡ซ๐Ÿ‡ฐ Falkland Islands +500
๐Ÿ‡ซ๐Ÿ‡ด Faroe Islands +298
๐Ÿ‡ซ๐Ÿ‡ฏ Fiji +679
๐Ÿ‡ซ๐Ÿ‡ฎ Finland +358
๐Ÿ‡ซ๐Ÿ‡ท France +33
๐Ÿ‡ฌ๐Ÿ‡ซ French Guiana +594
๐Ÿ‡ต๐Ÿ‡ซ French Polynesia +689
๐Ÿ‡ฌ๐Ÿ‡ฆ Gabon +241
๐Ÿ‡ฌ๐Ÿ‡ฒ Gambia +220
๐Ÿ‡ฌ๐Ÿ‡ช Georgia +995
๐Ÿ‡ฉ๐Ÿ‡ช Germany +49
๐Ÿ‡ฌ๐Ÿ‡ญ Ghana +233
๐Ÿ‡ฌ๐Ÿ‡ฎ Gibraltar +350
๐Ÿ‡ฌ๐Ÿ‡ท Greece +30
๐Ÿ‡ฌ๐Ÿ‡ฑ Greenland +299
๐Ÿ‡ฌ๐Ÿ‡ฉ Grenada +1473
๐Ÿ‡ฌ๐Ÿ‡ต Guadeloupe +590
๐Ÿ‡ฌ๐Ÿ‡บ Guam +1671
๐Ÿ‡ฌ๐Ÿ‡น Guatemala +502
๐Ÿ‡ฌ๐Ÿ‡ฌ Guernsey +44
๐Ÿ‡ฌ๐Ÿ‡ณ Guinea +224
๐Ÿ‡ฌ๐Ÿ‡ผ Guinea-Bissau +245
๐Ÿ‡ฌ๐Ÿ‡พ Guyana +592
๐Ÿ‡ญ๐Ÿ‡น Haiti +509
๐Ÿ‡ญ๐Ÿ‡ณ Honduras +504
๐Ÿ‡ญ๐Ÿ‡ฐ Hong Kong +852
๐Ÿ‡ญ๐Ÿ‡บ Hungary +36
๐Ÿ‡ฎ๐Ÿ‡ธ Iceland +354
๐Ÿ‡ฎ๐Ÿ‡ณ India +91
๐Ÿ‡ฎ๐Ÿ‡ฉ Indonesia +62
๐Ÿ‡ฎ๐Ÿ‡ท Iran +98
๐Ÿ‡ฎ๐Ÿ‡ถ Iraq +964
๐Ÿ‡ฎ๐Ÿ‡ช Ireland +353
๐Ÿ‡ฎ๐Ÿ‡ฒ Isle of Man +44
๐Ÿ‡ฎ๐Ÿ‡ฑ Israel +972
๐Ÿ‡ฎ๐Ÿ‡น Italy +39
๐Ÿ‡จ๐Ÿ‡ฎ Ivory Coast +225
๐Ÿ‡ฏ๐Ÿ‡ฒ Jamaica +1
๐Ÿ‡ฏ๐Ÿ‡ต Japan +81
๐Ÿ‡ฏ๐Ÿ‡ช Jersey +44
๐Ÿ‡ฏ๐Ÿ‡ด Jordan +962
๐Ÿ‡ฐ๐Ÿ‡ฟ Kazakhstan +7
๐Ÿ‡ฐ๐Ÿ‡ช Kenya +254
๐Ÿ‡ฐ๐Ÿ‡ฎ Kiribati +686
๐Ÿ‡ฝ๐Ÿ‡ฐ Kosovo +383
๐Ÿ‡ฐ๐Ÿ‡ผ Kuwait +965
๐Ÿ‡ฐ๐Ÿ‡ฌ Kyrgyzstan +996
๐Ÿ‡ฑ๐Ÿ‡ฆ Laos +856
๐Ÿ‡ฑ๐Ÿ‡ป Latvia +371
๐Ÿ‡ฑ๐Ÿ‡ง Lebanon +961
๐Ÿ‡ฑ๐Ÿ‡ธ Lesotho +266
๐Ÿ‡ฑ๐Ÿ‡ท Liberia +231
๐Ÿ‡ฑ๐Ÿ‡พ Libya +218
๐Ÿ‡ฑ๐Ÿ‡ฎ Liechtenstein +423
๐Ÿ‡ฑ๐Ÿ‡น Lithuania +370
๐Ÿ‡ฑ๐Ÿ‡บ Luxembourg +352
๐Ÿ‡ฒ๐Ÿ‡ด Macau +853
๐Ÿ‡ฒ๐Ÿ‡ฌ Madagascar +261
๐Ÿ‡ฒ๐Ÿ‡ผ Malawi +265
๐Ÿ‡ฒ๐Ÿ‡พ Malaysia +60
๐Ÿ‡ฒ๐Ÿ‡ป Maldives +960
๐Ÿ‡ฒ๐Ÿ‡ฑ Mali +223
๐Ÿ‡ฒ๐Ÿ‡น Malta +356
๐Ÿ‡ฒ๐Ÿ‡ญ Marshall Islands +692
๐Ÿ‡ฒ๐Ÿ‡ถ Martinique +596
๐Ÿ‡ฒ๐Ÿ‡ท Mauritania +222
๐Ÿ‡ฒ๐Ÿ‡บ Mauritius +230
๐Ÿ‡พ๐Ÿ‡น Mayotte +262
๐Ÿ‡ฒ๐Ÿ‡ฝ Mexico +52
๐Ÿ‡ซ๐Ÿ‡ฒ Micronesia +691
๐Ÿ‡ฒ๐Ÿ‡ฉ Moldova +373
๐Ÿ‡ฒ๐Ÿ‡จ Monaco +377
๐Ÿ‡ฒ๐Ÿ‡ณ Mongolia +976
๐Ÿ‡ฒ๐Ÿ‡ช Montenegro +382
๐Ÿ‡ฒ๐Ÿ‡ธ Montserrat +1664
๐Ÿ‡ฒ๐Ÿ‡ฆ Morocco +212
๐Ÿ‡ฒ๐Ÿ‡ฟ Mozambique +258
๐Ÿ‡ฒ๐Ÿ‡ฒ Myanmar +95
๐Ÿ‡ณ๐Ÿ‡ฆ Namibia +264
๐Ÿ‡ณ๐Ÿ‡ท Nauru +674
๐Ÿ‡ณ๐Ÿ‡ต Nepal +977
๐Ÿ‡ณ๐Ÿ‡ฑ Netherlands +31
๐Ÿ‡ณ๐Ÿ‡จ New Caledonia +687
๐Ÿ‡ณ๐Ÿ‡ฟ New Zealand +64
๐Ÿ‡ณ๐Ÿ‡ฎ Nicaragua +505
๐Ÿ‡ณ๐Ÿ‡ช Niger +227
๐Ÿ‡ณ๐Ÿ‡ฌ Nigeria +234
๐Ÿ‡ณ๐Ÿ‡บ Niue +683
๐Ÿ‡ณ๐Ÿ‡ซ Norfolk Island +6723
๐Ÿ‡ฐ๐Ÿ‡ต North Korea +850
๐Ÿ‡ฒ๐Ÿ‡ฐ North Macedonia +389
๐Ÿ‡ฒ๐Ÿ‡ต Northern Mariana Islands +1670
๐Ÿ‡ณ๐Ÿ‡ด Norway +47
๐Ÿ‡ด๐Ÿ‡ฒ Oman +968
๐Ÿ‡ต๐Ÿ‡ฐ Pakistan +92
๐Ÿ‡ต๐Ÿ‡ผ Palau +680
๐Ÿ‡ต๐Ÿ‡ฆ Panama +507
๐Ÿ‡ต๐Ÿ‡ฌ Papua New Guinea +675
๐Ÿ‡ต๐Ÿ‡พ Paraguay +595
๐Ÿ‡ต๐Ÿ‡ช Peru +51
๐Ÿ‡ต๐Ÿ‡ญ Philippines +63
๐Ÿ‡ต๐Ÿ‡ฑ Poland +48
๐Ÿ‡ต๐Ÿ‡น Portugal +351
๐Ÿ‡ต๐Ÿ‡ท Puerto Rico +1
๐Ÿ‡ถ๐Ÿ‡ฆ Qatar +974
๐Ÿ‡ซ๐Ÿ‡ท Rรฉunion +262
๐Ÿ‡ท๐Ÿ‡ด Romania +40
๐Ÿ‡ท๐Ÿ‡บ Russia +7
๐Ÿ‡ท๐Ÿ‡ผ Rwanda +250
๐Ÿ‡ง๐Ÿ‡ฑ Saint-Barthรฉlemy +590
๐Ÿ‡ธ๐Ÿ‡ญ Saint Helena +290
๐Ÿ‡ฐ๐Ÿ‡ณ Saint Kitts & Nevis +1869
๐Ÿ‡ฑ๐Ÿ‡จ Saint Lucia +1758
๐Ÿ‡ซ๐Ÿ‡ท Saint Martin +590
๐Ÿ‡ต๐Ÿ‡ฒ Saint Pierre & Miquelon +508
๐Ÿ‡ป๐Ÿ‡จ Saint Vincent & Grenadines +1784
๐Ÿ‡ผ๐Ÿ‡ธ Samoa +685
๐Ÿ‡ธ๐Ÿ‡ฒ San Marino +378
๐Ÿ‡ธ๐Ÿ‡น Sรฃo Tomรฉ & Prรญncipe +239
๐Ÿ‡ธ๐Ÿ‡ฆ Saudi Arabia +966
๐Ÿ‡ธ๐Ÿ‡ณ Senegal +221
๐Ÿ‡ท๐Ÿ‡ธ Serbia +381
๐Ÿ‡ธ๐Ÿ‡จ Seychelles +248
๐Ÿ‡ธ๐Ÿ‡ฑ Sierra Leone +232
๐Ÿ‡ธ๐Ÿ‡ฌ Singapore +65
๐Ÿ‡ธ๐Ÿ‡ฝ Sint Maarten +1721
๐Ÿ‡ธ๐Ÿ‡ฐ Slovakia +421
๐Ÿ‡ธ๐Ÿ‡ฎ Slovenia +386
๐Ÿ‡ธ๐Ÿ‡ง Solomon Islands +677
๐Ÿ‡ธ๐Ÿ‡ด Somalia +252
๐Ÿ‡ฟ๐Ÿ‡ฆ South Africa +27
๐Ÿ‡ฐ๐Ÿ‡ท South Korea +82
๐Ÿ‡ธ๐Ÿ‡ธ South Sudan +211
๐Ÿ‡ช๐Ÿ‡ธ Spain +34
๐Ÿ‡ฑ๐Ÿ‡ฐ Sri Lanka +94
๐Ÿ‡ธ๐Ÿ‡ฉ Sudan +249
๐Ÿ‡ธ๐Ÿ‡ท Suriname +597
๐Ÿ‡ณ๐Ÿ‡ด Svalbard & Jan Mayen +47
๐Ÿ‡ธ๐Ÿ‡ฟ Swaziland +268
๐Ÿ‡ธ๐Ÿ‡ช Sweden +46
๐Ÿ‡จ๐Ÿ‡ญ Switzerland +41
๐Ÿ‡ธ๐Ÿ‡พ Syria +963
๐Ÿ‡น๐Ÿ‡ผ Taiwan +886
๐Ÿ‡น๐Ÿ‡ฏ Tajikistan +992
๐Ÿ‡น๐Ÿ‡ฟ Tanzania +255
๐Ÿ‡น๐Ÿ‡ญ Thailand +66
๐Ÿ‡น๐Ÿ‡ฑ Timor-Leste +670
๐Ÿ‡น๐Ÿ‡ฌ Togo +228
๐Ÿ‡น๐Ÿ‡ฐ Tokelau +690
๐Ÿ‡น๐Ÿ‡ด Tonga +676
๐Ÿ‡น๐Ÿ‡น Trinidad & Tobago +1868
๐Ÿ‡น๐Ÿ‡ณ Tunisia +216
๐Ÿ‡น๐Ÿ‡ท Turkey +90
๐Ÿ‡น๐Ÿ‡ฒ Turkmenistan +993
๐Ÿ‡น๐Ÿ‡จ Turks & Caicos Islands +1649
๐Ÿ‡น๐Ÿ‡ป Tuvalu +688
๐Ÿ‡ป๐Ÿ‡ฎ U.S. Virgin Islands +1340
๐Ÿ‡บ๐Ÿ‡ฌ Uganda +256
๐Ÿ‡บ๐Ÿ‡ฆ Ukraine +380
๐Ÿ‡ฆ๐Ÿ‡ช United Arab Emirates +971
๐Ÿ‡ฌ๐Ÿ‡ง United Kingdom +44
๐Ÿ‡บ๐Ÿ‡ธ United States +1
๐Ÿ‡บ๐Ÿ‡พ Uruguay +598
๐Ÿ‡บ๐Ÿ‡ฟ Uzbekistan +998
๐Ÿ‡ป๐Ÿ‡บ Vanuatu +678
๐Ÿ‡ป๐Ÿ‡ฆ Vatican City +39
๐Ÿ‡ป๐Ÿ‡ช Venezuela +58
๐Ÿ‡ป๐Ÿ‡ณ Vietnam +84
๐Ÿ‡ผ๐Ÿ‡ซ Wallis & Futuna +681
๐Ÿ‡ช๐Ÿ‡ญ Western Sahara +212
๐Ÿ‡พ๐Ÿ‡ช Yemen +967
๐Ÿ‡ฟ๐Ÿ‡ฒ Zambia +260
๐Ÿ‡ฟ๐Ÿ‡ผ Zimbabwe +263

Advertisement

Answer

Maybe,

Ss([^rn]*?)s*+[0-9]+$

might return the country names in the capturing group $1.


Using lookaround, we can likely write some expression similar to:

S[A-Za-zรฉรฃ.].*(?=s+[0-9])

which we would get the second letter using,

[A-Za-zรฉรฃ.]   

prior to which, there is another S, and we would then bypass the emojis.

Demo 2

const regex = /Ss([^rn]*?)s*+[0-9]+$/gm;
const str = `๐Ÿ‡ฆ๐Ÿ‡ซ Afghanistan +93
๐Ÿ‡ฆ๐Ÿ‡ฝ ร…land Islands +358
๐Ÿ‡ฆ๐Ÿ‡ฑ Albania +355
๐Ÿ‡ฉ๐Ÿ‡ฟ Algeria +213
๐Ÿ‡ฆ๐Ÿ‡ธ American Samoa +1684
๐Ÿ‡ฆ๐Ÿ‡ฉ Andorra +376
๐Ÿ‡ฆ๐Ÿ‡ด Angola +244
๐Ÿ‡ฆ๐Ÿ‡ฎ Anguilla +1264`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

If you wish to simplify/modify/explore the expression, it’s been explained on the top right panel of regex101.com. If you’d like, you can also watch in this link, how it would match against some sample inputs.


User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement