JavaScript RegExp {x,y} Quantifier

The {X, Y} quantifier encompasses and corresponds to numbers within the range of x to y. This regex quantifier functions within a string of numbers through the regular expression's search, match, test, and exec functions. To work with the quantifier, we can apply regex modifiers to the specific characters.

Syntax

To retrieve the characters that are available, a regular expression quantifier {x,y} is utilized in the syntax.

Example

/a{x,y}/
Example

/a{x,y}/modifier;

Description

  • The "a" is represented as an input character with the modifier.
  • The "x" shows the minimum numbers of the sequence input string.
  • The "y" shows the range of the maximum numbers of the sequence input string.
  • The global and case-insensitive modifiers work with the regex quantifier.
  • Supported Browsers

The given browsers support the javascript regex {x,y} quantifier.

  • Google Chrome
  • Internet Explorer
  • Opera
  • Safari
  • Firefox
  • Edge
  • Examples

In the forthcoming instances, we will demonstrate the application of the {x,y} quantifier in JavaScript regex. It is possible to utilize numerical values as characters within this quantifier.

Example1:

The following example demonstrates the range of values for the character "j" within a string. It illustrates the possible values that the input data can take on.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp quantifier

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 370px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp {x,y} quantifier

</h3>

<h4> The regex quantifier matches for a sequence of the given range:

</h4>

<div id = "regex_quant"></div>

<div id = "regex_quant1"></div>

<!-- Click  button for {x,y}quantifier output -->

<button onclick = "display_group();"> Click Here! </button>

</div>

<script>

function display_group() {

var sequance_pattern1 = "java, php, jquery, javascript, nodejs";

// Use {0,5}quantifier regex

let input_pattern = /\j{0,5}/g;

var quant_sequance1 = sequance_pattern1.match(input_pattern); 

var ele = document.getElementById("regex_quant");

ele.innerHTML = "Available Values for the 0-5 range: "+quant_sequance1;

// Use {1,6}quantifier regex to match values

let input_pattern1 = /\j{1,6}/g;

var quant_sequance2 = sequance_pattern1.match(input_pattern1); 

// show output for the array values of the range 

var ele1 = document.getElementById("regex_quant1");

ele1.innerHTML = "Available Values for the 1-6 range: "+quant_sequance2;

}

</script>

</body>

</html>

Output

The result displays the value representing the extent of the quantifier.

Example2:

The provided illustration exhibits the range values that are accessible for the input data through the utilization of the match method.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp quantifier

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 370px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp {x,y} quantifier

</h3>

<h4> The regex quantifier matches for a sequence of the given range:

</h4>

<div id = "regex_quant"></div>

<div id = "regex_quant1"></div>

<!-- Click  button for {x,y}quantifier output -->

<button onclick = "display_group ();"> Click Here! </button>

</div>

<script>

function display_group() {

var sequance_pattern1 = "2, 0, 200, 2000, 10000";

// Use {1,3}quantifier regex to match numbers

let input_pattern = /\d{1,3}/g;

var quant_sequance1 = sequance_pattern1.match(input_pattern); 

var ele = document.getElementById("regex_quant");

ele.innerHTML = "Available Values for the 1-3 range: "+quant_sequance1;

// Use {1,6}quantifier regex to match range

let input_pattern1 = /\d{1,6}/g;

var quant_sequance2 = sequance_pattern1.match(input_pattern1); 

// show output for the available values of the range 

var ele1 = document.getElementById("regex_quant1");

ele1.innerHTML = "Available Values for the 1-6 range: "+quant_sequance2;

}

</script>

</body>

</html>

Output

The result displays the quantifier's range value.

Example 3:

The provided example demonstrates how to check the range of numerical values within a string. By employing the test method, we can ascertain the range of values present in the input data.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp quantifier

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 370px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp {x,y} quantifier

</h3>

<h4> The regex quantifier tests for a sequence of the range of the numbers:

</h4>

<div id = "regex_quant"></div>

<div id = "regex_quant1"></div>

<!-- Click  button for {x,y}quantifier output -->

<button onclick = "display_group();"> Click Here! </button>

</div>

<script>

function display_group() {

var sequance_pattern1 = "2, 0, 200, 2000, 100";

// Use {num1,num2}quantifier regex to match given numbers range

let input_pattern = /\d{0,6}/g;

var quant_sequance1 = input_pattern.test(sequance_pattern1); 

var ele = document.getElementById("regex_quant");

ele.innerHTML = "Available Values for the 0-6 range:: "+quant_sequance1;

// Use regex to match the given number range

let input_pattern1 = /\d{6,7}/g;

var quant_sequance2 = input_pattern1.test(sequance_pattern1);  

// show output for the availability of the range 

var ele1 = document.getElementById("regex_quant1");

ele1.innerHTML = "Available Values for for the 6-7 range:: "+quant_sequance2;

}

</script>

</body>

</html>

Output

The result displays the testing value for the quantifier's range.

Example 4:

The following example illustrates how to determine the range of numerical values within a string and substitute the necessary information.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp quantifier

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 370px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp {x,y} quantifier

</h3>

<h4> The regex quantifier matches for a sequence of the given range:

</h4>

<div id = "regex_quant"></div>

<div id = "regex_quant1"></div>

<!-- Click  button for {x,y}quantifier output -->

<button onclick = "display_group();"> Click Here!

</button>

</div>

<script>

function display_group() {

var sequance_pattern1 = "2, 0, 200, 2000, 10000";

// Use regex to match given numbers (3 to 6) range

let input_pattern = /\d{3,6}/g;

let replace = "$";

let quant_sequance1 = sequance_pattern1.replace(input_pattern, replace);

var ele = document.getElementById("regex_quant");

ele.innerHTML = "Available Values for the 3-6 range: "+quant_sequance1;

let input_pattern1 = /\d{5,6}/g;

let replace2 = "3";

let quant_sequance2 = sequance_pattern1.replace(input_pattern1, replace2);

// show output for the replace value of the range 

var ele1 = document.getElementById("regex_quant1");

ele1.innerHTML = "Available Values for the 1-6 range: "+quant_sequance2;

}

</script>

</body>

</html>

Output

The result displayed represents the value that replaces the range specified by the quantifier.

Example 5:

The provided example demonstrates how to determine the position of a value within a string.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp quantifier

</title>

<style>

#demo1{

background-color: orange;

border: 1px solid black;

width: 370px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp {x,y} quantifier

</h3>

<h4> The regex quantifier matches for a sequence of the given range:

</h4>

<div id = "regex_quant"></div>

<div id = "regex_quant1"></div>

<!-- Click  button for {x,y}quantifier output -->

<button onclick = "display_group ();"> Click Here! </button>

</div>

<script>

function display_group() {

var sequance_pattern1 = "2, 0, 200, 2000, 10000";

// Use quantifier regex to search numbers

let input_pattern = /\d{2,3}/g;

var quant_sequance1 = sequance_pattern1.search(input_pattern); 

var ele = document.getElementById("regex_quant");

ele.innerHTML = "Available Values for the 1-3 range: "+quant_sequance1;

let input_pattern1 = /\d{4,6}/g;

var quant_sequance2 = sequance_pattern1.search (input_pattern1); 

// show output for the index position of the range 

var ele1 = document.getElementById("regex_quant1");

ele1.innerHTML = "Available Values for the 1-6 range: "+quant_sequance2;

}

</script>

</body>

</html>

Output

The output displays the location of the quantifier's range.

Conclusion

The "x,y" quantifier in JavaScript allows specifying a range of occurrences between a minimum of x and a maximum of y in a regular expression.

Input Required

This code uses input(). Please provide values below: