Open In App
Related Articles

JavaScript RegExp Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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>
<html lang="en">
<head>
    <title>
        JavaScript RegExp Reference
    </title>
</head>
  
<body style="text-align:center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h2>RegExp . Metacharacter</h2>
    <p>
        Input String: ABC, A3C, A C, AXXCC!
    </p>
    <button onclick="geek()">
        Click it!
    </button>
    <p id="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.

ConstructorDescriptionExample
RegExp()It creates a new RegExp object
Try

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 PropertiesDescriptionExample
constructorReturn the RegExp constructor function for the object.
Try
dotAllIt checks if the ‘s’ flag is used or not in the Regular Expression
Try
flagsThis property returns the flag being used in the current RegExp object
Try
globalIt specifies whether the ‘g’ flag is used or not
Try
hasIndicesIt specifies whether the ‘d’ flag is used or not
Try
ignoreCaseIt specifies whether the ‘i’ flag is used or not
Try
multilineIt specifies whether ‘m’ flag is set or not
Try
sourceIt returns the text of the RegExp object 
Try
stickyIt specifies whether ‘y’ flag is used or not
Try
unicodeIt specifies whether ‘u’ flag is used or not
Try
lastIndexIt specifies at which index to start the next match
Try

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 MethodsDescriptionExample
execIt is used to test if the strings match
Try
testIt also checks if the string matches but the return type is different
Try
toStringIt returns the string value of the Regular Expression
Try

The different Metacharacters and Flags of RegExp is given below:

RegExp

Description

Example

MetacharacterSearch single characters, except line terminator or newline.
Try
 m  Perform multiline matching.
Try
 \rThe RegExp \r Metacharacter carriage return characters.
Try
 [abc]Search for any character which is between the brackets.
Try
 (x|y)Search any of the specified characters (separated by |).
Try
 \xxxFind the character specified by an octal number xxx.
Try
 \W Find the non word character i.e. characters which are not from a to z, A to Z, 0 to 9. 
Try
 [^abc]Search for any character which is not between the brackets.
Try
 gFind all the occurrences of the pattern instead of stopping after the first match i.e it performs a global match.
Try
 [0-9] Search any digit which is between the brackets.
Try
 \sFind the whitespace characters. 
Try
 \bFind a match at the beginning or end of a word
Try
 i Perform case-insensitive matching in the string.
Try
 \nThe RegExp \n Metacharacter in JavaScript is used to find the newline character.
Try
 [^0-9] Search for any digit which is not between the brackets.
Try
 \BFind a match that is not present at the beginning or end of a word.
Try
 \fFind the form feed character (form feed is a page-breaking ASCII control character).
Try
 \wFind 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].
Try
 \dSearch digit characters. It is the same as [0-9].
Try
 \t If it is found it returns the position else it returns -1.
Try
 \DSearch non-digit characters i.e all the characters except digits. It is the same as [^0-9].
Try
 \0Find the NULL character. If it is found it returns the position else it returns -1.
Try
 \vFind the vertical tab character. If it is found it returns the position else it returns -1.
Try
 *Find the match of any string that contains zero or more occurrences of m.
Try
 {X,}Find the match of any string that contains a sequence of m, at least X times, where X is a number.
Try
 ?!Find the match of any string which is not followed by a specific string m.
Try
 {X}Find the match of any string that contains a sequence of m, X times where X is a number.
Try
 ^Find the match of any string which contains m at the beginning of it.
Try
 ?Find the match of any string that contains zero or one occurrence of m.
Try
 $Find the match of any string which contains m at the end of it.
Try
 +Find the match of any string that contains at least one m.
Try
 \uxxxxFind the Unicode character specified by a hexadecimal number XXXX.
Try
 {X,Y}Find the match of any string that contains a sequence of m, X to Y times where X, Y must be numbered.
Try


Last Updated : 30 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads