A regular expression refers to the object which represents the pattern of characters.
JavaScript provides the RegEx class which provides the pattern-matching and search-and-replace functionality on text.
Syntax:
/pattern/modifiers
Modifiers
Modifiers are used to perform case-insensitive and global searches.
| Modifier | Description |
|---|---|
| g | It is used to find all matches. |
| i | It is used to perform case-insensitive matching |
| m | It is used to perform multi-line matching. |
Brackets
Brackets are used to find the range of characters.
| Expression | Description |
|---|---|
| [abc] | It is used to find any character between the brackets. |
| [^abc] | It is used to find any character not between the brackets. |
| [0-9] | It is used to find any character between the brackets (any digit). |
| [^0-9] | It is used to find any character not between the brackets (any non-digit) |
| (x|y) | It is used to find any of the alternatives specified. |
Metacharacters
Metacharacters are the alphabetical characters preceded by a backslash with a special meaning.
| Metacharacter | Description |
|---|---|
| . | It is used to find a single character. It excludes newline or line terminator |
| \w | It is used to find a word character. |
| \W | It is used to find a non-word character. |
| \d | It is used to find a digit. |
| \D | It is used to find a non-digit character. |
| \s | It is used to find a white space character. |
| \S | It is used to find a non-whitespace character. |
| \b | It is used to find a match at the beginning/end of a word. |
| \B | It is used to find a match not at the beginning/end of a word. |
| \0 | It is used to find a NULL character. |
| \n | It is used to find a new line character. |
| \f | It is used to find a form feed character. |
| \r | It is used to find a carriage return character. |
| \t | It is used to find a tab character. |
| \v | It is used to find a vertical tab character. |
| \xxx | It is used to find the character specified by an octal number xxx. |
| \xdd | It is used to find the character specified by a hexadecimal number dd. |
| \udddd | It is used to find the Unicode character specified by a hexadecimal number dddd. |
Quantifiers
| Quantifier | Description |
|---|---|
| n+ | It matches any string that contains at least one n. |
| n* | It matches any string that contains zero or more occurrences of n. |
| n? | It matches any string that contains zero or one occurrence of n. |
| n{X} | It matches any string that contains a sequence of X n‘s. |
| n{X,Y} | It matches any string that contains a sequence of X to Y n‘s. |
| n{X,} | It matches any string that contains a sequence of at least X n‘s. |
| n$ | It matches any string with n at the end of it. |
| ^n | It matches any string with n at the beginning of it. |
| ?=n | It matches any string that is followed by a specific string n. |
| ?!n | It matches any string that is not followed by a specific string n. |
RegExp Object Properties
| Property | Description |
|---|---|
| constructor | It returns the function that created the RegExp object’s prototype. |
| global | It is used to check whether the “g” modifier is set. |
| ignoreCase | It is used to check whether the “i” modifier is set. |
| lastIndex | It is used to get the index at which to start the next match. |
| multiline | It is used to check whether the “m” modifier is set. |
| source | It is used to get the text of the RegExp pattern. |
RegExp Object Methods
RegExp.exec() Method:
Use: To get an array containing all the matched groups for a specified string.
RegExp.test() Method:
Use: To search for a match between a regular expression and a specified string.
RegExp.toString() Method:
Use: To retrieve a string to represent the regular expression.
Example:
<!DOCTYPE html>
<html>
<body>
<script>
var string = "HELLO WORLD";
var regExObj = new RegExp( "HELLO", "g" );
var print = regExObj.exec(string);
document.write("Returned value : " + print);
</script>
</body>
</html>