Skip to content

Instantly share code, notes, and snippets.

@jakub-g
Last active December 22, 2022 08:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jakub-g/e580331ce7363b831d4273fb2fded4d9 to your computer and use it in GitHub Desktop.
Save jakub-g/e580331ce7363b831d4273fb2fded4d9 to your computer and use it in GitHub Desktop.
Unicode-aware JavaScript regex cheat sheet

Unicode-aware JavaScript regex (Unicode property escapes /\p{..}\P{..}/u) cheat sheet

Browser support MDN

High level intro

  • \p{...} in a regex with /u flag is a positive match
  • \P{...} in a regex with /u flag is a negative match

Unicode-aware [a-zA-Z]

'Gérard'    .match(/\p{Letter}+/gu)     // ["Gérard"]
'Łódź'      .match(/\p{Letter}+/gu)     // ["Łódź"]
'Википедия' .match(/\p{Letter}+/gu)     // ["Википедия"]

Unicode-aware \w ([a-zA-Z0-9_]+)

/([\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]+)/gu

Unicode-aware \W (^\w)

/[^\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]/gu

Unicode-aware \b

⚠ WIP ⚠

\b is a word boundary for words defined like \w. AFAIU there's no short and good equivalent which works for non-ASCII. You probably want to be more specific with your regex and rely on ^, $, \s and other characters.

Matching emoji

/\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F/gu

Sources

JavaScript RegEx named capture groups

Browser support caniuse

Description

It's possible to name the capturing group (...) via (?<name>...) syntax and later access the match via match.groups.name, instead of match[0], match[1] etc.

var re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
console.log(re.exec("1999-02-29").groups.year)
@saleebm
Copy link

saleebm commented Jun 28, 2020

You can check Firefox now. The issue closed 2 months ago.

@jakub-g
Copy link
Author

jakub-g commented Jun 30, 2020

@saleebm It's been closed 2 months ago but only released to stable channel today as Firefox 78. I updated the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment