Skip to content

Instantly share code, notes, and snippets.

@ruandre
Last active June 25, 2022 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruandre/b3b3c3403c2c544465d7a5ccf25999d4 to your computer and use it in GitHub Desktop.
Save ruandre/b3b3c3403c2c544465d7a5ccf25999d4 to your computer and use it in GitHub Desktop.
RegEx Examples
// \s - any whitespace character (including \t, \n and a few others)
// \S - any non-whitespace character
// \w - any word character (uppercase and lowercase latin alphabet, numbers 0-9, and _)
// \W - any non-word character
// \b - word boundary (boundaries between \w and \W, matches in-between characters)
// \B - non-word boundary (inverse of \b)
// (?!) - negative lookahead
// (?=) - positive lookahead
// (?<=) - positive lookbehind
// (?<!) - negative lookbehind
vconst { log } = console
const s = 'See the parrot. And the goat. And the rooster.'
log(s.match(/\w+?(?=\.)/g))
// Matches []:
// See the [parrot]. And the [goat]. And the [rooster].
// (word before period/full-stop)
log(s.match(/the (?=goat)/g))
// Matches []:
// See the parrot and [the ]goat and the rooster.
// ("the " only before "goat")
log(s.match(/the (?!goat)/g))
// Matches []:
// See [the ]parrot and the goat and [the ]rooster.
// ("the " except before "goat")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment