How to find character between brackets in JavaScript RegExp?



In this tutorial, we will learn how we can find a character between the brackets in JavaScript RegExp with examples.

Syntax

Following is the syntax that you have to follow for finding the character between the brackets of JavaScript RegExp −

[……]

In the above syntax, you can put the character or the combination of characters that you are searching in place of dots while the square brackets are the basic syntax to indicate the use of JavaScript RegExp.

Below is the list of different syntaxes that you can use to check for the presence of different characters in the brackets −

  • [x] − This syntax will check for the presence of the only character x in the sentence.
  • [xyz] − This syntax will give you the result for the presence of any of the characters among a, b, c, or a combination of all of them i.e. abc.
  • [A-Z] − This syntax is used to check for the presence of any character in the range from uppercase A and uppercase Z.
  • [a-z] − This syntax is used to check for the presence of any character in the range from lowercase a and lowercase z.
  • [A-z] − This syntax is used to check for the presence of any character in the range from uppercase A and lowercase z.

Let us discuss all of them with coding examples one by one.

Algorithm

  • Step 1 − We define a sentence in that we check for the character between the brackets of JavaScript RegExp.
  • Step 2 − Next we will define the regular expression for a particular character, the combination of characters or range of characters that you will check for, according to the syntax discussed above.
  • Step 3 − In the next step, we will use the in-built match() function or method of JavaScript to match the original sentence with the regular expression defined by you.
  • Step 4 − In the last step, we will display the sentence obtained after matching the original sentence with a regular expression.

Example 1

The below example will illustrate how we can use the above syntax to search for a particular character i.e. 't' that is present in the brackets of JavaScript RegExp −

<!DOCTYPE html> <html> <body> <h3>Find a character between the brackets in JavaScript RegExp</h3> <p>It will do a global search for the characters that are "t":</p> <p>Original Sentence: tutorials point simply easy learning.</p> <p id="result"></p> <script> let myStr = "tutorials point simply easy learning"; let reg = /[t]/g; let match = myStr.match(reg); document.getElementById("result").innerHTML = match; </script> </body> </html>

The example we discussed above will give you the output by printing t, the number of times it appears in the original sentence. It will match the regular expression with the original sentence and notes the number of times t appears in it, and then it will print t number of times it appears.

Example 2

The below example will explain the use of syntax to find a multiple characters (a, b, c) individually or combined that are in brackets of JavaScript RegExp −

<!DOCTYPE html> <html> <body> <h3>Find a character between the brackets in JavaScript RegExp</h3> <p>It will do a global search for the characters that are "a, b, c":</p> <p>Original Sentence: Hey there, we are checking for character between the brackets of JavaScript RegExp</p> <p id="result"></p> <script> let myStr = "Hey there, we are checking for character, not between the brackets of JavaScript RegExp."; let reg = /[abc]/g; let match = myStr.match(reg); document.getElementById("result").innerHTML = match; </script> </body> </html>

The above example will output the number of times a, b, and c appear either individually or combined in the sentence. It uses the JavaScript match function to get the number of times each character appears in the sentence and then prints all of the time.

Example 3

The below example will illustrate how we can use the above syntax to find the characters in a particular range that are in brackets of JavaScript RegExp at one time −

<!DOCTYPE html> <html> <body> <h3>Find a character between the brackets in JavaScript RegExp</h3> <p>It will do a global search for the characters that are in the range "A-Z":</p> <p>Original Sentence: Hey THere, We are Checking for CHaracter Between the Brackets of JavaScript RegExp</p> <p id="result"></p> <script> let myStr = "Hey THere, We are Checking for CHaracter Between the Brackets of JavaScript RegExp."; let reg = /[A-Z]/g; let match = myStr.match(reg); document.getElementById("result").innerHTML = match; </script> </body> </html>

In above example, we have used the syntax to find the appearances of all the characters that are ranging from A-Z. It will check for the characters that are in the defined range and get number of times each character appears using match method and after that it prints each character individually according to their appearance in left to right order.

In above discussion, we have learnt how to find the character between the brackets of the JavaScript RegExp with the help of three different examples which are individually belongs to a different syntax of finding the characters.


Advertisements