Match specific word in regex in JavaScript?


The task is to match a specific word or character which is in regex with a string.

The regex (regular expressions) is a pattern that is used to match character combinations in strings. Here we include the test(), match(), and matchAll() methods to match the following word in regex.

We have some boundary-type assertions, in which we have used \b.

Consider a sentence – “mickey is holding mic.”

Using regex - \bmic\b will match the word mic but not the word mic in mickey. It is a word boundary. Another assertion is (g), It is a global search flag.

Consider a sentence – “me and me are mine with me”.

const sentence = 'me and me are mine with me'; const regex = /me/; document.write(sentence.match(regex)); //me

In the above situation, we haven’t given (g) into regex, so it will return only first matching value.

const sentence = 'me and me are mine with me'; const regex = /me/g; document.write(sentence.match(regex)); //me, me, me

Here, we have given (g) into regex, so it returned all the “me” word in the entire sentence.

Using test() method

The test() method will search for the matching word between regex and the input string. This method will return a Boolean value as output.

Example

Following is the example, to match the word which is in regex with the input string −

<!DOCTYPE html> <html> <head> <title>Match specific word in regex in JavaScript</title> </head> <body> <script> var line = "Karthikeya is massive hit in hindi belt"; var regex = /\bmassive\b/g; var res = regex.test(line); document.write(res); </script> </body> </html>

As we can see in the below output, it returned the value which is in regex by comparing it with the string sentence.

By using match() method

The match() method will return the match against the string. This will return the output in different types −

  • If there is any match in the string, it will return an array with the match value.

  • If there isn’t any match in the string, it will return Null.

Example 1

Let’s look into the example below, where it is returning the match value as output and Null if no match.

<!DOCTYPE html> <html> <head> <title>Match specific word in regex in JavaScript</title> </head> <body> <script> const line = 'RRR became famous all over the globe'; const regex1 = /RRR/; const regex2 = /KGF/; document.write(line.match(regex1),"<br>"); //RRR document.write(line.match(regex2)); //Null </script> </body> </html>

As we can see in the below output, it returned the matched value as an array and Null as the regex value is not matching anywhere in the string −

Example 2

Using global flag (g)

As we discussed above, if we give the (g) to the regex value. It will iterate the entire string sentence and will return the matching word everywhere it is presents in the string sentence.

<!DOCTYPE html> <html> <head> <title>Match specific word in regex in JavaScript</title> </head> <body> <script> const line = 'Rajamouli is the reason for RRR becaming famous all over the globe'; const regex1 = /the/g; document.write(line.match(regex1),"<br>"); //RRR </script> </body> </html>

In the below output, by using the global flag (g) it returned the match value everywhere occurred in the input string sentence.

Example 3

Using case insensitive search flag (i)

Another assertion is (i), which is a case-insensitive search flag.

If we use this (i) case-insensitive search flag in regex. It will return all the match value whether it is in upper case or lower case.

Following is the example where we matched regex with a string irrespective of upper case and lower case.

<!DOCTYPE html> <html> <head> <title>Match specific word in regex in JavaScript</title> </head> <body> <script> const Dhoni = 'We are all servents and we are doing national duty.'; const regex1 = /we/gi; document.write(Dhoni.match(regex1)); </script> </body> </html>

As we can see in the output given below, it returned all the values which are matching with regex irrespective of lower and uppercase.

Using matchAll() method

The global search flag (g) is necessary for .matchAll(), and it either returns an iterator or an empty array.

We need to use spread(…) operator to get the elements from the string as a series of values.

Example

Following is the example to match a word in regex with a specified string sentence by using matchAll() method −

<!DOCTYPE html> <html> <head> <title>Match specific word in regex in JavaScript</title> </head> <body> <script> const Dhoni = 'We are all servents and we are doing national duty.'; const regex1 = /we/gi; document.write(...Dhoni.matchAll(regex1)); </script> </body> </html>

In the output, the matchAll() method returned all the values in the entire string sentence which are matched with regex

Updated on: 22-Sep-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements