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!

JavaScript

Advertisement

Answer

Maybe,

JavaScript

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


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

JavaScript

which we would get the second letter using,

JavaScript

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

Demo 2

JavaScript

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