Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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