Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

RegExp

JavaScript RegExp objects allow authors to work with regular expressions. Regular expressions work similarly in most scripting languages. We supply them with two paramters: (1)the text pattern to match for, and (2)optional flags. Special characters can also be applied to your patterns for enabling a wide range of logic within your regular expressions.

Regular expressions are used with several String methods of JavaScript such as match(), replace(), search(), split(). RegExp objects sport built-in properties, methods and special characters that you can explore on this page.

var regex = /pattern/; var regex = new RegExp("pattern");

Both approaches above will establish a similar regular expression in JavaScript. The literal approach uses forward slashes around the text pattern, while the constructor approach uses quote marks and separates the parameters. If you know the pattern is static and will not change use a literal regular expression to maximize script performance. If you know that the pattern will be dynamic, use the second approach that calls the constructor and explicitly creates a RegExp object.

This next example shows applying flags to your regular expression. We apply a global(g) flag along with an ignore case(i) flag to the regular expression so that all matches of the word "bird" are found and the text casing is not a factor in the pattern match.

var regex = /bird/gi; var regex = new RegExp("bird","gi");

Flags

You can apply one or more flags to your patterns in order to alter the match logic.

g - (global) target all matches. var str = "Joe sells cogs from his cog store."; var regex = /cog/g; var result = str.replace(regex, "bed"); document.write(result); // Joe sells beds from his bed store. i - ignore the casing of letters. var str = "I want to fIGhT."; var regex = /fight/i; var result = str.replace(regex, "fart"); document.write(result); // I want to fart. m - (multiline) performs match over multiple lines("\n" represents a new line). var str = "pop pop\npop pop"; var regex = /p$/gm; var result = str.replace(regex, "X"); document.write(result); // pop poX pop poX

Special Characters

Consider these placeholder values before you explore the special characters: abc = primary pattern to match, replace 'abc' with your pattern. i / j = integers, replace 'i' and 'j' with integers where you see them below.

\ - escapes the normal functionality of special characters to interpret them literally. Syntax: /abc\+/ ^ - match pattern where it is the beginning of input (newlines affected using the g and m flags). Syntax: /^abc/ $ - match pattern where it is the end of input (newlines affected using the g and m flags). Syntax: /abc$/ * - match pattern 0 or more times. Syntax: /abc*/ + - match pattern 1 or more times. Syntax: /abc+/ ? - match pattern 1 or 0 times. Syntax: /abc?/ . - match everything except newline(\n) characters. Syntax: /./ ( ) - match and store into resulting array. Syntax: /(abc)/ (?: ) - match and do not store into resulting array. Syntax: /(?:abc)/ (?= ) - match 'abc' only if it precedes 'def'. Syntax: /abc(?=def)/ (?! ) - match 'abc' only if it does not precede 'def'. Syntax: /abc(?!def)/ | - match either 'abc' or 'def'. Syntax: /abc|def/ {i} - match the pattern 'i' times. Syntax: /abc{i}/ {i,} - match the pattern at least but not limited to 'i' times. Syntax: /abc{i,}/ {i,j} - match the pattern at least 'i' times, and at the most 'j' times. Syntax: /abc{i,j}/ [ ] - match the range of characters specified within the brackets. Syntax: /[hijklmnop]/ same as /[h-p]/ [^ ] - match what is not in the character set. Syntax: /[^hijklmnop]/ same as /[^h-p]/ \b - match where 'abc' starts a new word. Syntax: /\babc/ \B - match where a word ends with 'abc'. Syntax: /\Babc/ \c - match a control character ( A - Z ). Syntax: /\cA/ \d - match digit found in a string, same as using [0-9]. Syntax: /\d/ \D - match non-digit found in a string, same as using [^0-9]. Syntax: /\D/ \f - match form feed characters. Syntax: /\f/ \n - match newline characters. Syntax: /\n/ \r - match carriage return characters. Syntax: /\r/ \s - match white space, newline, return and tab, same as using [ \n\r\t]. Syntax: /\s/ \S - match non-white space characters, same as using [^ \n\r\t]. Syntax: /\S/ \t - match tab characters. Syntax: /\t/ \v - match vertical tab. Syntax: /\v/ \w - match any alphanumeric character, same as using [a-zA-Z0-9_]. Syntax: /\w/ \W - match any non-alphanumeric character, same as using [^a-zA-Z0-9_]. Syntax: /\W/

Properties

lastindex - returns the next index position that follows the last match made. var regex = /a/g; var result = regex.test("cat"); document.write(result+" : "+regex.lastIndex); // true : 2 source - returns the text of the pattern without its forward slashes. var regex = /abc/; document.write(regex.source); // abc

Methods

exec - returns the matched pattern if it is found, and returns 'null' if not found. var str = "Birds fly in the sky."; var regex = /fly/; var result = regex.exec(str); document.write(result); // fly test - returns 'true' if a match is found, and returns 'false' if not found. var str = "abc 123"; var regex = /bc 12/; var result = regex.test(str); document.write(result); // true toString - returns a string representing the RegExp object. var regex = /abc/; var result = regex.toString(); document.write(result); // /abc/

JavaScript

Objects

ArgumentsArrayAudioBooleanDataTransferDateDocumentElementFunctionGlobalHistoryHTMLElementMathNavigatorNumberObjectRegExpScreenStorageStringWindowXMLHttpRequest