Find digits between brackets in JavaScript RegExp?



In this tutorial, we learn how to find the digits between brackets using JavaScript RegExp. The ASCII values for the digits (0-9) start from 48 to 57. We denote the digits in the bracket as [0-9] in the regular expression. To find the digits in a range except for all digits, we can write the particular range. As to find the digits between 4 and 8, we could write it as [4-8] in the regular expression pattern. Now, our aim is to find digits inside the brackets in the text using RegExp in JavaScript. We could follow the below syntaxes to find the digits between brackets.

Syntax

Following is the syntax for RegExp group [0-9] character −

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 like g, i, m. "g" for performing global matches, "i" for performing case-insensitive matching and "m" for performing multiline matching.

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

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

Algorithm

  • STEP 1 − Define a string having some digits.
  • STEP 2 − Define the RegExp pattern for digits between brackets.
  • STEP 3 − Apply match(pattern) on the above-defined string to find digits between brackets in the string.
  • STEP 4 − Display the result- the matched digits.

Let’s look at some program examples for more clear 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

Finding and Replacing Digits between Brackets

In the example below, we find and replace the digits between 1 and 4 with the space character. We use the split() and join() methods for this purpose.

<!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.


Advertisements