JS RegExp

JS Tutorial

JS Version

JS Objects

JS Function

JS Classes

JS Async

JS HTML DOM

JS Browser BOM

JS Web API

JS AJAX

JS JSON

JS vs JQUERY

JS Graphics

JavaScript Regular Expressions

A regular expression is defined as a sequence of characters that result in formation of a search pattern.

The search pattern can be used for text search and text replace operations.

What Is a Regular Expression?

A regular expression is defined as a sequence of characters that generates a search pattern.

When data is searched in a text, this search pattern is used to describe what you are searching for.

A regular expression can be a single character or a more complicated pattern.

Regular expressions perform all types of text search and text replace operations.

Syntax

/pattern/modifiers;

Example

/tutorial/i;

Example explained:

/tutorial;/i  is a regular expression.

A tutorial is a pattern (to be used in a search).

i  is a modifier (modifies the search to be case-insensitive).

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 find a match and returns the position of the match.

The replace() method is used to return a modified string where the pattern is replaced.

Using String search() With a String

The search() method is used to search a string for a defined value and returns the position of the match:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript String Methods</h2>

<p>Search a string for “Webhostguru”, and display the position of the match:</p>

<p id=”demo”></p>

<script>

let text = “Visit Webhostguru!”;

let n = text.search(“Webhostguru”);

document.getElementById(“demo”).innerHTML = n;

</script>

</body>

</html>

Output

JavaScript String Methods

Search a string for "Webhostguru", and display the position of the match:

6

Using String replace() With a String

The replace() method is used to replaces a specified value with another value in a string.

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript String Methods</h2

<p>Replace “Microsoft” with “Webhostguru” in the paragraph below:</p>

<button onclick=”myFunction()”>Try it</button

<p id=”demo”>Please visit Microsoft!</p>

<script>

function myFunction() {

let text = document.getElementById(“demo”).innerHTML;

 document.getElementById(“demo”).innerHTML =

 text.replace(“Microsoft”,”Webhostguru”);

}

</script>

</body>

</html>

Output

JavaScript String Methods

Replace "Microsoft" with "Webhostguru" in the paragraph below:

Please visit Microsoft!

Use String replace() With a Regular Expression

Example

Using a case insensitive regular expression to replace Microsoft with tutorial in a string:

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript String Methods</h2>

<p>Replace “Microsoft” with “Webhostguru” in the paragraph below:</p>

<button onclick=”myFunction()”>Try it</button>

<p id=”demo”>Please visit Microsoft!</p>

<script>

function myFunction() {

  let text = document.getElementById(“demo”).innerHTML;

 document.getElementById(“demo”).innerHTML =

  text.replace(/microsoft/i, “Webhostguru”);

}

</script>

</body>

</html>

Output

JavaScript String Methods

Replace "Microsoft" with "Webhostguru" in the paragraph below:

Please visit Microsoft!

Using test()

The test() method is a RegExp expression method.

It finds a string for a pattern, and returns true or false, depending on the result.

The following example searches a string for the character “e”:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Regular Expressions</h2>

<p>Search for an “e” in the next paragraph:</p>

<p id=”p01″>The best things in life are free!</p>

<p id=”demo”></p>

<script>

let text = document.getElementById(“p01”).innerHTML;

const pattern = /e/;

document.getElementById(“demo”).innerHTML = pattern.test(text);

</script>

</body>

</html>

Output

JavaScript Regular Expressions

Search for an "e" in the next paragraph:

The best things in life are free!

true

Using exec()

The exec() method is a RegExp expression method.

It finds a string for a definite pattern, and returns the found text as an object.

If no match is found, it returns an empty (null) object.

The following example searches a string for the character “e”:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Regular Expressions</h2>

<p id=”demo”></p>

<script>

const obj = /e/.exec(“The best things in life are free!”);

document.getElementById(“demo”).innerHTML =

“Found ” + obj[0] + ” in position ” + obj.index + ” in the text: ” + obj.input;

</script>

</body>

</html>

Output

JavaScript Regular Expressions

Found e in position 2 in the text: The best things in life are free!