如何使用 JavaScript RegExp 执行不区分大小写的匹配?

javascriptprogramming scriptshtml

在本教程中,我们将学习如何使用 JavaScript RegExp 执行不区分大小写的匹配。

正则表达式可以用两种方式声明 -

  • 使用正则表达式文字,以斜杠开头和结尾,模式放在中间。
  • 调用 RegExp 对象构造函数,该构造函数采用参数中的模式和标志来创建正则表达式。

用户可以使用以下语法创建正则表达式。

语法

//使用正则表达式文字
const regex = /tutorial/i
//使用 RegExp 构造函数
const regex2 = new RegExp('tutorial', 'i')

在上述语法中,正则表达式用于匹配单词"tutorial",修饰符"i"表示它可以匹配任何包含这些字符的子字符串,而不管其大小写如何("TuToRial"、"Tutorial"等)。

使用 String match() 方法

match() 方法是 JavaScript 中 String 对象的一部分。它用于将字符串与 RegExp 或正则表达式进行匹配。

用户可以按照以下语法使用 match() 方法通过 JavaScript RegExp 执行不区分大小写的匹配。

语法

text.match(regex)

在上述语法中,"text"是需要使用正则表达式进行检查的字符串。 "regex" 是正则表达式模式。

参数

  • regex − 它是正则表达式或将转换为正则表达式的字符串。

返回类型

  • 返回所有匹配项的数组,如果未找到匹配项,则返回 null。

示例

在下面给出的示例中,我们使用 match() 方法执行不区分大小写的匹配。我们正在检查按钮单击时的匹配方法结果并将其输出。

<html> <body> <h4>Performming Case Insensitive Matching with RegExp using <i> match() </i> method</h4> <button onclick="check()">Check</button> <p>Original Text: Welcome to Tutorialspoint</p> <p>Text To Match: tutorial </p> <p id="output"></p> <script> const text='Welcome to Tutorialspoint' const regex=/tutorial/i function check(){ //Using the match method let result=text.match(regex) document.getElementById('output').innerHTML='Mached Text: '+result } </script> </body> </html>

以上输出显示 match() 方法返回匹配的子字符串"Tutorial"。

使用字符串 search() 方法

search() 方法是 JavaScript 中 String 对象的一部分。它用于根据 RegExp 或正则表达式搜索字符串的子字符串。

用户可以按照以下语法使用 search() 方法使用 JavaScript RegExp 执行不区分大小写的匹配。

语法

text.search(regex)

在上述语法中,"text"是字符串,"regex"是正则表达式模式。

参数

  • regex − 它是正则表达式或将转换为正则表达式的字符串。

返回类型

  • 返回第一个匹配的位置,如果未找到匹配项,则返回 -1。

示例

在下面给出的示例中,我们使用了 search()方法并检查按钮单击时 search() 方法的结果并将其输出。

<html> <body> <h4>Performming Case Insensitive Matching with RegExp using <i> search() </i> method.</h4> <p>Text: Welcome to Tutorialspoint</p> <p>Text to Match: tutorial</p> <button onclick="check()">Check</button> <p id="output"></p> <p><b>Note:</b>The search() method returns the position of first match</p> <script> const text='Welcome to Tutorialspoint' const regex=/tutorial/i function check(){ //Using search method let result=text.search(regex) document.getElementById('output').innerHTML='Result: '+result } </script> </body </html>

在上面的输出中,用户可以看到search()方法返回子字符串"Tutorial"的起始位置。

使用RegExp test()方法

test()方法是JavaScript中RegExp对象的一部分。它用于根据RegExp或正则表达式测试字符串。

用户可以按照以下语法使用test()方法使用JavaScript RegExp执行不区分大小写的匹配。

语法

regex.test(text)

在上面的语法中,"text"是一个需要使用正则表达式检查的字符串。 "regex" 是正则表达式模式。

参数

  • 文本/字符串 − 它是需要测试的文本或字符串。

返回类型

  • 如果未找到匹配项,则返回 false,否则返回 true。

示例

在下面给出的示例中,我们使用了 test() 方法。

<html> <body> <p>Performming Case Insensitive Matching with JavaScript RegExp using <i> test() </i> method</p> <p>Text: Welcome to Tutorialspoint</p> <p>Text to Match: tutorial</p> <button onclick="check()">Check</button> <p id="output"></p> <p><b>Note:</b> The test() method returns true if there is a match, else returns false.</p> <script> const text = 'Welcome to Tutorialspoint' const regex = /tutorial/i function check() { //Using the test method let result = regex.test(text) document.getElementById('output').innerHTML = 'Result: ' + result } </script> </body> </html>

在上面的输出中,用户可以看到 test() 方法返回 true,因为文本中存在"Tutorial"子字符串。

使用 RegExp exec() 方法

exec() 方法是 JavaScript 中 RegExp 对象的一部分。它用于将字符串与 RegExp 或正则表达式进行匹配。

用户可以按照以下语法使用 exec() 方法通过 JavaScript RegExp 执行不区分大小写的匹配。

语法

regex.exec(text)

在上述语法中,"text"是字符串,"regex"是正则表达式模式。

参数

  • Text/string − 它是需要匹配的文本或字符串。

返回类型

  • 返回所有匹配项的数组,如果未找到匹配项,则返回 null。

示例

在下面给出的示例中,我们使用了 exec() 方法。

<html> <body> <p>Performming Case Insensitive Matching with JavaScript RegExp using <i> exec() </i> method</p> <button onclick="check()">Check</button> <p>Text: Welcome to Tutorialspoint</p> <p id="output"></p> <script> const text='Welcome to Tutorialspoint' const regex=/tutorial/i function check(){ //Using the exec method let result=regex.exec(text) document.getElementById('output').innerHTML='Result: '+result } </script> </body> </html>

上面的输出显示 exec() 方法返回匹配的子字符串"Tutorial"。

在本教程中,我们讨论了使用 RegExp 执行不区分大小写的匹配的四种方法。前两种方法是字符串 match() 和 search() 方法。另外两种方法是 RegExp test() 和 exec() 方法。


相关文章