Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Match specific word in regex in JavaScript?
In JavaScript, matching specific words using regex (regular expressions) is a common task for string pattern matching. This article covers the main methods to match words: test(), match(), and matchAll().
Understanding Word Boundaries
The \b assertion represents a word boundary, ensuring you match complete words rather than partial matches within other words.
<!DOCTYPE html>
<html>
<head>
<title>Word Boundary Example</title>
</head>
<body>
<script>
const sentence = "mickey is holding mic.";
const regex = /\bmic\b/;
document.write("Full word 'mic' found: " + regex.test(sentence));
</script>
</body>
</html>
Full word 'mic' found: true
This matches "mic" as a complete word but not the "mic" within "mickey".
Global Flag Behavior
Without the global flag (g), regex returns only the first match:
<!DOCTYPE html>
<html>
<head>
<title>Single Match Example</title>
</head>
<body>
<script>
const sentence = 'me and me are mine with me';
const regex = /me/;
document.write("First match: " + sentence.match(regex));
</script>
</body>
</html>
First match: me
With the global flag (g), it finds all matches:
<!DOCTYPE html>
<html>
<head>
<title>Global Match Example</title>
</head>
<body>
<script>
const sentence = 'me and me are mine with me';
const regex = /me/g;
document.write("All matches: " + sentence.match(regex));
</script>
</body>
</html>
All matches: me,me,me
Using test() Method
The test() method returns a boolean value indicating whether the pattern matches the string.
<!DOCTYPE html>
<html>
<head>
<title>Test Method Example</title>
</head>
<body>
<script>
var line = "Karthikeya is massive hit in hindi belt";
var regex = /\bmassive\b/g;
var result = regex.test(line);
document.write("Pattern found: " + result);
</script>
</body>
</html>
Pattern found: true
Using match() Method
The match() method returns an array with match details or null if no match is found.
Basic Match Example
<!DOCTYPE html>
<html>
<head>
<title>Match Method Example</title>
</head>
<body>
<script>
const line = 'RRR became famous all over the globe';
const regex1 = /RRR/;
const regex2 = /KGF/;
document.write("Match RRR: " + line.match(regex1) + "<br>");
document.write("Match KGF: " + line.match(regex2));
</script>
</body>
</html>
Match RRR: RRR Match KGF: null
Using Global Flag (g)
<!DOCTYPE html>
<html>
<head>
<title>Global Match Example</title>
</head>
<body>
<script>
const line = 'Rajamouli is the reason for RRR becoming famous all over the globe';
const regex1 = /the/g;
document.write("All 'the' matches: " + line.match(regex1));
</script>
</body>
</html>
All 'the' matches: the,the
Using Case Insensitive Flag (i)
The i flag performs case-insensitive matching:
<!DOCTYPE html>
<html>
<head>
<title>Case Insensitive Example</title>
</head>
<body>
<script>
const sentence = 'We are all servants and we are doing national duty.';
const regex1 = /we/gi;
document.write("Case insensitive matches: " + sentence.match(regex1));
</script>
</body>
</html>
Case insensitive matches: We,we
Using matchAll() Method
The matchAll() method requires the global flag (g) and returns an iterator with detailed match information. Use the spread operator to convert results to an array.
<!DOCTYPE html>
<html>
<head>
<title>MatchAll Example</title>
</head>
<body>
<script>
const sentence = 'We are all servants and we are doing national duty.';
const regex1 = /we/gi;
const matches = [...sentence.matchAll(regex1)];
document.write("Matches found: " + matches.length + "<br>");
matches.forEach(match => document.write("Match: " + match[0] + " at position " + match.index + "<br>"));
</script>
</body>
</html>
Matches found: 2 Match: We at position 0 Match: we at position 24
Method Comparison
| Method | Return Type | Global Flag Support | Use Case |
|---|---|---|---|
test() |
Boolean | Yes | Check if pattern exists |
match() |
Array or null | Yes | Get matched strings |
matchAll() |
Iterator | Required | Get detailed match info |
Conclusion
Use test() for boolean checks, match() for simple string extraction, and matchAll() when you need detailed information about each match including position and capture groups.
