JavaScript RegExp Reference

RegExp stands for Regular Expression. A regular expression is a sequence of characters that forms a search pattern. The search pattern can be used for text search and text to replace operations. A regular expression can be a single character or a more complicated pattern.
Syntax:
new RegExp("(Regular Expressioncharactersnonword)")
Example: This example searches the words whose starting character is āAā and ending character is āCā with only one character in between them.
HTML
| <!DOCTYPE html> <htmllang="en"> <head> Ā Ā Ā Ā <title> Ā Ā Ā Ā Ā Ā Ā Ā JavaScript RegExp Reference Ā Ā Ā Ā </title> </head> Ā Ā<bodystyle="text-align:center"> Ā Ā Ā Ā <h1style="color:green"> Ā Ā Ā Ā Ā Ā Ā Ā zambiatek Ā Ā Ā Ā </h1> Ā Ā Ā Ā <h2>RegExp . Metacharacter</h2> Ā Ā Ā Ā <p> Ā Ā Ā Ā Ā Ā Ā Ā Input String: ABC, A3C, A C, AXXCC! Ā Ā Ā Ā </p> Ā Ā Ā Ā <buttononclick="geek()"> Ā Ā Ā Ā Ā Ā Ā Ā Click it! Ā Ā Ā Ā </button> Ā Ā Ā Ā <pid="app"></p> Ā Ā Ā Ā <script> Ā Ā Ā Ā Ā Ā Ā Ā function geek() { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let str1 = "ABC, A3C, A C, AXXCC!"; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let regex4 = /A.C/g; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let match4 = str1.match(regex4); Ā ĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.getElementById("app").innerHTML = Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Found " + match4.length Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + " matches: " + match4; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā </script> </body> </html> | 
Output:
 
Ā
The complete list of JavaScript RegExpĀ is listed below:
JavaScript RegExp Constructor: In JavaScript, a constructor gets called when an object is created using the new keyword.
| Constructor | Description | Example | 
|---|---|---|
| RegExp() | It creates a new RegExp object | 
JavaScript RegExp Properties: A JavaScript property is a member of an object that associates a key with a value.
- Instance Property: An instance property is a property that has a new copy for every new instance of the class.
| Instance Properties | Description | Example | 
|---|---|---|
| constructor | Return the RegExp constructor function for the object. | |
| dotAll | It checks if the āsā flag is used or not in the Regular Expression | |
| flags | This property returns the flag being used in the current RegExp object | |
| global | It specifies whether the āgā flag is used or not | |
| hasIndices | It specifies whether the ādā flag is used or not | |
| ignoreCase | It specifies whether the āiā flag is used or not | |
| multiline | It specifies whether āmā flag is set or not | |
| source | It returns the text of the RegExp objectĀ | |
| sticky | It specifies whether āyā flag is used or not | |
| unicode | It specifies whether āuā flag is used or not | |
| lastIndex | It specifies at which index to start the next match | 
JavaScript RegExp Methods: JavaScript methods are the actions that can be performed on objects.
- Instance Method: If the method is called on an instance of a RegExp then it is called an instance method.
| Instance Methods | Description | Example | 
|---|---|---|
| exec | It is used to test if the strings match | |
| test | It also checks if the string matches but the return type is different | |
| toString | It returns the string value of the Regular Expression | 
The different Metacharacters and Flags of RegExp is given below:
| RegExp | Description | Example | 
|---|---|---|
| Metacharacter | Search single characters, except line terminator or newline. | |
| Ā mĀ | Ā Perform multiline matching. | |
| Ā \r | The RegExp \r Metacharacter carriage return characters. | |
| Ā [abc] | Search for any character which is between the brackets. | |
| Ā (x|y) | Search any of the specified characters (separated by |). | |
| Ā \xxx | Find the character specified by an octal number xxx. | |
| Ā \WĀ | Find the non word character i.e. characters which are not from a to z, A to Z, 0 to 9.Ā | |
| Ā [^abc] | Search for any character which is not between the brackets. | |
| Ā g | Find all the occurrences of the pattern instead of stopping after the first match i.e it performs a global match. | |
| Ā [0-9]Ā | Search any digit which is between the brackets. | |
| Ā \s | Find the whitespace characters.Ā | |
| Ā \b | Find a match at the beginning or end of a word | |
| Ā iĀ | Perform case-insensitive matching in the string. | |
| Ā \n | The RegExp \n Metacharacter in JavaScript is used to find the newline character. | |
| Ā [^0-9]Ā | Search for any digit which is not between the brackets. | |
| Ā \B | Find a match that is not present at the beginning or end of a word. | |
| Ā \f | Find the form feed character (form feed is a page-breaking ASCII control character). | |
| Ā \w | Find the word character i.e. characters from a to z, A to Z, 0 to 9. It is the same as [a-zA-Z_0-9]. | |
| Ā \d | Search digit characters. It is the same as [0-9]. | |
| Ā \tĀ | If it is found it returns the position else it returns -1. | |
| Ā \D | Search non-digit characters i.e all the characters except digits. It is the same as [^0-9]. | |
| Ā \0 | Find the NULL character. If it is found it returns the position else it returns -1. | |
| Ā \v | Find the vertical tab character. If it is found it returns the position else it returns -1. | |
| Ā * | Find the match of any string that contains zero or more occurrences of m. | |
| Ā {X,} | Find the match of any string that contains a sequence of m, at least X times, where X is a number. | |
| Ā ?! | Find the match of any string which is not followed by a specific string m. | |
| Ā {X} | Find the match of any string that contains a sequence of m, X times where X is a number. | |
| Ā ^ | Find the match of any string which contains m at the beginning of it. | |
| Ā ? | Find the match of any string that contains zero or one occurrence of m. | |
| Ā $ | Find the match of any string which contains m at the end of it. | |
| Ā + | Find the match of any string that contains at least one m. | |
| Ā \uxxxx | Find the Unicode character specified by a hexadecimal number XXXX. | |
| Ā {X,Y} | Find the match of any string that contains a sequence of m, X to Y times where X, Y must be numbered. | 
 
				 
					


