在JavaScript中使用正则表达式匹配特定单词?


任务是使用正则表达式匹配字符串中的特定单词或字符。

正则表达式 (regex) 是一种用于匹配字符串中字符组合的模式。这里我们包括test()match()matchAll() 方法来匹配正则表达式中的以下单词。

我们有一些边界类型断言,其中我们使用了 \b。

考虑一个句子:“mickey is holding mic.”

使用正则表达式 - \bmic\b 将匹配单词mic,但不匹配mickey中的mic。这是一个单词边界。另一个断言是(g),它是一个全局搜索标志。

考虑一个句子:“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

在上述情况下,我们没有在正则表达式中使用 (g),因此它只返回第一个匹配值。

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

这里,我们在正则表达式中使用了 (g),所以它返回了整个句子中所有“me”单词。

使用 test() 方法

test() 方法将在正则表达式和输入字符串之间搜索匹配的单词。此方法将返回布尔值作为输出。

示例

以下是一个示例,用于将正则表达式中的单词与输入字符串匹配:

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

正如我们在下面的输出中看到的,它通过将正则表达式与字符串句子进行比较,返回了正则表达式中的值。

使用 match() 方法

match() 方法将返回与字符串匹配的结果。这将以不同类型返回输出:

  • 如果字符串中存在任何匹配项,它将返回包含匹配值的数组。

  • 如果字符串中没有任何匹配项,它将返回 Null。

示例 1

让我们看看下面的示例,它将匹配值作为输出返回,如果无匹配则返回 Null。

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

正如我们在下面的输出中看到的,它将匹配值作为数组返回,如果正则表达式的值在字符串中任何地方都不匹配,则返回 Null:

示例 2

使用全局标志 (g)

正如我们上面所讨论的,如果我们将 (g) 提供给正则表达式的值,它将迭代整个字符串句子,并在字符串句子中出现匹配的单词的任何地方返回它。

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

在下面的输出中,通过使用全局标志 (g),它返回了输入字符串句子中出现的任何地方的匹配值。

示例 3

使用不区分大小写的搜索标志 (i)

另一个断言是 (i),它是不区分大小写的搜索标志。

如果我们在正则表达式中使用此 (i) 不区分大小写的搜索标志,它将返回所有匹配值,无论它们是大写还是小写。

以下是一个示例,我们不考虑大小写的情况下将正则表达式与字符串匹配。

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

正如我们在下面给出的输出中看到的,它返回了所有与正则表达式匹配的值,而不考虑大小写。

使用 matchAll() 方法

全局搜索标志 (g) 对.matchAll() 是必需的,它要么返回迭代器,要么返回空数组。

我们需要使用展开运算符 (…) 来将字符串中的元素作为一系列值获取。

示例

以下是如何使用matchAll() 方法将正则表达式中的单词与指定的字符串句子匹配的示例:

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

在输出中,matchAll() 方法返回整个字符串句子中所有与正则表达式匹配的值。

更新于:2022年9月22日

浏览量 10K+

启动您的职业生涯

完成课程后获得认证

开始学习
广告
© . All rights reserved.