JavaScript RegExp Objects

Back to JS page

Table of Contents

The RegExp Object

The RegExp object is used to search for patterns in strings. It can be created with literal syntax /pattern/ or new RegExp():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript RegExp Objects</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The RegExp Object</h4>
<p id="demo"></p>

<script>
// Literal syntax
const pattern1 = /hello/;

// Constructor syntax
const pattern2 = new RegExp("hello");

let text = "Literal pattern: " + pattern1 + "<br>";
text += "Constructor pattern: " + pattern2 + "<br><br>";
text += "Both are RegExp objects: " + (pattern1 instanceof RegExp) + "<br>";
text += "with global flag: " + /hello/g.global;

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 1

Result View Example

Using test()

The test() method searches a string for a pattern match. It returns true or false:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript RegExp Objects</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Using test()</h4>
<p id="demo"></p>

<script>
const text = "The rain in Spain";

// test() returns boolean
const result1 = /ain/.test(text);
const result2 = /xyz/.test(text);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "test(/ain/): " + result1 + " (found)<br>" +
  "test(/xyz/): " + result2 + " (not found)<br><br>" +
  "test() returns true or false";
</script>

</body>
</html>

Example 2

Result View Example

Using exec()

The exec() method searches for a match and returns the match details (or null):

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript RegExp Objects</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Using exec()</h4>
<p id="demo"></p>

<script>
const text = "The rain in Spain";

// exec() returns match details
const result = /ain/.exec(text);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "exec(/ain/):<br>" +
  "match[0]: " + result[0] + "<br>" +
  "index: " + result.index + "<br>" +
  "input: " + result.input;
</script>

</body>
</html>

Example 3

Result View Example

The RegExp.escape() Method

RegExp.escape() escapes special characters in a string so it can be used as a literal pattern:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript RegExp Objects</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The RegExp.escape() Method</h4>
<p id="demo"></p>

<script>
let text = "";

if (RegExp.escape) {
  // Escape special characters
  const escaped = RegExp.escape("a.b*c");
  text += "RegExp.escape('a.b*c'): " + escaped + "<br><br>";
  
  // Use escaped pattern to match literal dots and stars
  const result = "a.b*c".match(new RegExp(escaped));
  text += "Matching literal 'a.b*c': " + result[0];
} else {
  text += "RegExp.escape() is not supported in this browser yet.";
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 4

Result View Example

Document

Document in project

You can Download PDF file.

Reference