Home >Web Front-end >HTML Tutorial >Find numbers in brackets in JavaScript's RegExp?

Find numbers in brackets in JavaScript's RegExp?

WBOY
WBOYforward
2023-08-20 18:45:45806browse

Find numbers in brackets in JavaScripts RegExp?

In this tutorial, we learn how to find the number in brackets using JavaScript RegExp. Numbers (0-9) have ASCII values ​​from 48 to 57. We use [0-9] to represent the numbers in brackets in regular expressions. To find numbers in a range except all numbers, we can write a specific range. For example, to find a number between 4 and 8, we can write [4-8] in the regular expression pattern. Now, our goal is to find numbers inside brackets in text using RegExp in JavaScript. We can follow the following syntax to find the number in brackets.

Syntax

The following is the syntax of RegExp group [0-9] characters -

new RegExp("[0-9]") or simply /[0-9]/ 

/[0-9]/, is introduced in ES1. It is fully supported by all browsers. Like, Chrome, IE, Safari, Opera, FireFox and Edge.

RegExp has modifiers, such as g, i, m. "g" is used to perform global matching, "i" is used to perform case-insensitive matching, and "m" is used to perform multi-line matching.

Syntax for /[0-9]/ with modifier like

new RegExp("[0-9]", "g") or simply /[0-9]/g

Algorithm

  • Step 1 − Define a string containing some numbers.
  • STEP 2 − Define the RegExp pattern for digits between brackets.
  • Step 3 - Apply the match(pattern) function on the string defined above to find the numbers between brackets in the string.
  • Step 4 - Display the results - matching numbers.

Let's see some program examples for a clearer understanding.

Example 1

In the program below, we use string match(pattern) to find digits between 1 and 4 in the given string. We use RegExp pattern as /[1-4]/g. The string match() method returns an array of digits in the string.

<!DOCTYPE html>
<html>
<body>
   <h2>Finding digits inside the bracket</h2>
   <p id = "text"></p>
   <p>Digits inside the bracket [1-4] :
   <span id= "result"></span>
   </p>
   <script>
      let myStr = "0127845639Hello";
      document.getElementById("text").innerHTML = myStr;
      let pattern = /[1-4]/g;
      let result = myStr.match(pattern);
      document.getElementById("result").innerHTML = result;
   </script>
</body>
</html>

Here, text is given as 0-9 digits and Hello word. In the pattern, we have given [1-4] only. match() method will search digits from 1 to 4 only. If mentioned digits found in the text, match() method will return an array of existing digits otherwise it will return as null. Let's see another example.

Example 2

In the program below, we take a string with no digits and try to find digits in the string. We use string match(pattern) to find digits between 1 and 4 in the given string. We use the RegExp pattern as / [1-4]/g. See what our output looks like.

<!DOCTYPE html>
<html>
<body>
   <h1>Finding digits inside the bracket</h1>
   <p id= "result"></p>
   <script>
      let text = "567890";
      let pattern = /[1-4]/g;
      let result = text.match(pattern);
      if(result == null){
         document.getElementById("result").innerHTML = "Sorry, there is no digits in text that mentioned in the brackets";
      } else {
         ocument.getElementById("result").innerHTML = result;
      }
   </script>
</body>
</html>

Here, we can observe in the pattern we have mentioned [1-4] but in the text we are given from 5-9 and 0. match() method will return as null because there are no findings. So, if the statement is executed. If input text is given as the first example, then match() will return an array of existing digits and another statement will be executed. Like,

Example 3

<!DOCTYPE html>
<html>
<body>
   <h1>Finding digits inside the bracket</h1>
   <p id= "result"></p>
   <script>
      let text = "0127845639Hello";
      let pattern = /[1-4]/g;
      let result = text.match(pattern);
      if(result == null){
         document.getElementById("result").innerHTML = "Sorry, there is no digits in text that mentioned in the brackets";
      } else {
         document.getElementById("result").innerHTML = "Digit(s) inside the inside the bracket: " + result;
      }
   </script>
</body>
</html>

Now, We will check how to replace word character(s) in a given text. Let’s see an example

Example 4

Find and replace numbers between brackets

In the following example, we use the split() and join() methods to find and replace the numbers between 1 and 4 with space characters.

<!DOCTYPE html>
<html>
<body>
   <h1>Replace digits inside the bracket</h1>
   <p>After replacing the digits inside the bracket :
   <span id= "result"></span>
   </p>
   <script>
      let text = "0127845639Hello";
      let pattern = /[1-4]/g;
      let result = text.split(pattern).join(" ");
      document.getElementById("result").innerHTML = result;
   </script>
</body>
</html>

Example 5

We will also check to replace the digits inside the bracket using a simpler way. Like,

<!DOCTYPE html>
<html>
<body>
   <h1>Replace digits inside the bracket</h1>
   <p>After replacing the digits inside the bracket :
   <span id= "result"></span>
   </p>
   <script>
      let text = "0127845639Hello";
      let pattern = /[1-4]/g;
      let result = text.replace(pattern , " ");
      document.getElementById("result").innerHTML = result;
   </script>
</body>
</html>

As we discussed, g for global matches. Instead of stopping with the first occurrence, it will look for all the occurrences.

Hope this tutorial will give knowledge on how to find digits inside the brackets using RegExp in JavaScript.

The above is the detailed content of Find numbers in brackets in JavaScript's RegExp?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete