如何在 JavaScript RegExp 中執行不區分大小寫的匹配?


在本教程中,我們將學習如何在 JavaScript RegExp 中執行不區分大小寫的匹配。

正則表示式可以透過兩種方式宣告:

  • 使用正則表示式字面量,它以斜槓開頭和結尾,模式位於兩者之間。
  • 呼叫 RegExp 物件建構函式,它在引數中採用模式和標誌來建立正則表示式。

使用者可以使用以下語法建立正則表示式。

語法

//Using a regular expression literal
const regex = /tutorial/i
//Using RegExp constructor
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() 方法執行不區分大小寫的匹配。我們單擊按鈕檢查 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”。

使用 String 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”是正則表示式模式。

引數

  • Text/string − 它是需要測試的文字或字串。

返回型別

  • 如果找不到匹配項則返回 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() 方法。

更新於:2022年9月15日

3000+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.