JavaScript RegExp {X,Y} Quantifier
Last Updated :
28 Nov, 2024
Improve
JavaScript RegExp {X,Y} Quantifier in JavaScript is used to find the match of any string that contains a sequence of m, X to Y times where X, Y must be numbered.
// Matches between 2 and 4 digits
const regex = /\d{2,4}/;
console.log(regex.test("123"));
console.log(regex.test("12345"));
console.log(regex.test("12"));
Output
true true true
Syntax
/m{X, Y}/
// or
new RegExp("m{X, Y}")
Syntax with Modifiers
/\m{X, Y}/g
// or
new RegExp("m{X}", "g")
Example 1: Matching the presence of the word between [a-g] of length 3 to 4 in the whole string.
let str = "GeeksforGeeeeks@_123_$";
let regex = /[a-g]{3,4}/gi;
let match = str.match(regex);
console.log("Found " + match.length
+ " matches: " + match);
Output
Found 2 matches: Gee,Geee
Example 2: This example replaces the word ‘ee’ or ‘eee’ with ‘$’.
let str = "ee@128GeeeeK";
let regex = new RegExp("e{2,3}", "gi");
let replace = "$";
let match = str.replace(regex, replace);
console.log(" New string: " + match);
Output
New string: $@128G$eK
Supported Browsers
- Chrome
- Safari
- Firefox
- Opera
- Edge
We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.