Table of Contents
Quantifiers specify how many times a pattern should match:
| Quantifier | Description |
|---|---|
+ |
Match 1 or more times |
* |
Match 0 or more times |
? |
Match 0 or 1 times |
{n} |
Match exactly n times |
{n,m} |
Match between n and m times |
{n,} |
Match n or more times |
+ matches 1 or more occurrences of the preceding character/pattern:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Quantifiers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp + Quantifier</h4>
<p id="demo"></p>
<script>
const text = "hello world hello!!";
const matches = text.match(/o+/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/o+/g): " + matches + "<br>" +
"'+' matches one or more 'o' characters";
</script>
</body>
</html>

Result View Example
* matches 0 or more occurrences of the preceding pattern:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Quantifiers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp * Quantifier</h4>
<p id="demo"></p>
<script>
const text = "Hello World";
const matches = text.match(/lo*/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/lo*/g): " + matches + "<br>" +
"'*' matches zero or more 'o' characters after 'l'";
</script>
</body>
</html>

Result View Example
? matches 0 or 1 occurrence of the preceding character:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Quantifiers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp ? Quantifier</h4>
<p id="demo"></p>
<script>
const text = "color colour";
const matches = text.match(/colou?r/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/colou?r/g): " + matches + "<br>" +
"'?' makes 'u' optional (0 or 1)";
</script>
</body>
</html>

Result View Example
{n} matches exactly n occurrences:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Quantifiers</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp {n} Quantifier</h4>
<p id="demo"></p>
<script>
const phone = "555-123-4567";
const matches = phone.match(/\d{3}/g);
const match4 = phone.match(/\d{4}/g);
document.getElementById("demo").innerHTML =
"Text: " + phone + "<br><br>" +
"match(/\\d{3}/g): " + matches + " (exactly 3 digits)<br>" +
"match(/\\d{4}/g): " + match4 + " (exactly 4 digits)";
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.