JavaScript RegExp Characters

Back to JS page

Table of Contents

RegExp Character Classes

Character classes match any one of the characters inside square brackets [...].

Character Class [HW]

[HW] matches any single character in the set H or W:

<!DOCTYPE html>
<html>
<body>

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

<h4>Character Class [HW]</h4>
<p id="demo"></p>

<script>
const text = "HELLO world";
const matches = text.match(/[HW]/g);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "match(/[HW]/g): " + matches + "<br>" +
  "Matches any H or W character";
</script>

</body>
</html>

Example 1

Result View Example

Character Range [A-Z]

[A-Z] matches any uppercase letter from A to Z:

<!DOCTYPE html>
<html>
<body>

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

<h4>Character Range [A-Z]</h4>
<p id="demo"></p>

<script>
const text = "Hello World 123";
const matches = text.match(/[A-Z]/g);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "match(/[A-Z]/g): " + matches + "<br>" +
  "Matches any uppercase letter";
</script>

</body>
</html>

Example 2

Result View Example

Character Class [1234]

[1234] matches any single digit in the set 1, 2, 3, or 4:

<!DOCTYPE html>
<html>
<body>

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

<h4>Character Class [1234]</h4>
<p id="demo"></p>

<script>
const text = "123456789";
const matches = text.match(/[1234]/g);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "match(/[1234]/g): " + matches + "<br>" +
  "Matches digits 1, 2, 3, or 4 only";
</script>

</body>
</html>

Example 3

Result View Example

Character Range [1-4]

[1-4] matches any digit from 1 to 4 (same as [1234] but using a range):

<!DOCTYPE html>
<html>
<body>

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

<h4>Character Range [1-4]</h4>
<p id="demo"></p>

<script>
const text = "123456789";
const matches = text.match(/[1-4]/g);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "match(/[1-4]/g): " + matches + "<br>" +
  "Range syntax = same as [1234]";
</script>

</body>
</html>

Example 4

Result View Example

Document

Document in project

You can Download PDF file.

Reference