Skip to content

Instantly share code, notes, and snippets.

@frankie9793
Last active August 13, 2019 05:49
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 frankie9793/320f29665cae831aa6e87adca100b06b to your computer and use it in GitHub Desktop.
Save frankie9793/320f29665cae831aa6e87adca100b06b to your computer and use it in GitHub Desktop.

Javascript Regular Expressions

A regular expression or regex describes a pattern of characters. It can be used to perform powerful pattern-matching and search-and-replace functions on text.

Defining Regular Expressions

Regular expression literals let pattern = /s$/;

Regular Expression Object let pattern = new RegExp('s$');

Most characters (alphanumeric) describes characters to be matched literally. There are however metacharacters that have special significant meanings.

For example in the above case, the first 's' matches itself literally and the '$' is a special character that matches the end of the string. Hence the regular expression matches any string which contains 's' as its last character.

Literal Characters

JavaScript supports certain nonalphabetic characters through escape sequences that begin with backslash ().

Character Matches
Alphanumeric Character itself
\0 The NUL character
\t Tab
\n Newline
\v Vertical Tab
\f Form feed
\r Carriage return
\x nn The Latin Character specified by the hexadecimal number nn;eg \x0A === \n
\u xxxx The Unicode character specified by the hexadecimal number xxxx;eg \u0009 === \t
\c x The control character ^ ** x**;eg \cJ === \n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment