Regular Expression

A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for. A regular expression can be a single character, or a more complicated pattern. Regular expressions can be used to perform all types of text search and text replace operations.
 

Syntax:

/pattern/modifiers; 

Example:

var patt = /abcd/i;

Using String Methods

In JavaScript, regular expressions are often used with the two string methods: search() and replace().
The search() method uses an expression to search for a match, and returns the position of the match.
The replace() method returns a modified string where the pattern is replaced.Using String Methods

Try

Using String search() With a Regular Expression

Example:

<!DOCTYPE html>
<html>
<body>
<script>
    var str = "Visit abcd!"; 
    var n = str.search(/abcd/i);
    document.write("Position of match is:\t" + n);
</script>
</body>
</html>

Output:

Position of match is: 6 

 

Use String replace() With a Regular Expression

Example:

<!DOCTYPE html>
<html>
<body>
<p>Replace "microsoft" with "company" in the paragraph below:</p>
<p id="demo">Please visit Microsoft and Microsoft!</p>
<script>
    var str = document.getElementById("demo").innerHTML; 
    var txt = str.replace(/microsoft/i,"company");
    document.write(txt);
</script>
</body>
</html>


Output

Replace "microsoft" with "company" in the paragraph below:
Please visit Microsoft and Microsoft!
Please visit company and Microsoft! 

Use String replace() With a Regular Expression

Example:

<!DOCTYPE html>
<html>
<body>
<p>Replace "microsoft" with "company" in the paragraph below:</p>
<p id="demo">Please visit Microsoft and Microsoft!</p>
<script>
    var str = document.getElementById("demo").innerHTML; 
    var txt = str.replace(/microsoft/i,"company");
    document.write(txt);
</script>
</body>
</html>

Output

Replace "microsoft" with "company" in the paragraph below:
Please visit Microsoft and Microsoft!
Please visit company and Microsoft! 

Try

Regular Expression Modifiers

Modifiers are used in regular expression pattern to increase the scope of the search and make it more global.

Here is a list of the three modifiers

Modifier Description
i This modifier makes the search case insensitive
g This modifier searches for all the possibilities without stopping at the first match
m This modifier is used for multiline matching

Regex Patterns

Regex patterns are of three types.

Brackets: Brackets are used to define the range of searchable characters.

Pattern Description
[abcX] To find out any characters present in between the brackets
[0-9] To find out any numbers in the range of values provided within the brackets

 

Meta Characters: Every metacharacter has its special meaning

Pattern Description
\d To find a Digit
\s To find a whitespace character
\b To find a match at the beginning or the end of any word
\uxxxx To find the Unicode character specified by the hexadecimal number xxxx

 

Quantifiers: They define the quantities in the pattern

Pattern Description
n+ Matches a string that contains one or more than one K’s
n* Matches a string that contains zero or more than zero K’s
n? Matches a string that contains zero or one K