JavaScript 中 test() 和 exec() 方法的區別


RegExp.prototype.test()RegExp.prototype.exec() 方法是 JavaScript 中用於處理正則表示式的兩種方法。除了這兩種方法之外,JavaScript 還包含許多內建方法,用於使用正則表示式操作和處理文字。

什麼是正則表示式?

正則表示式是用於在字串中搜索字元組合的模式。在 JavaScript 中,正則表示式被視為物件,用 "regex" 或 "RegExp" 表示。

exec() 方法

exec() 方法在給定字串中搜索指定字串的匹配項。如果在給定字串中存在匹配項,則返回一個數組;如果在給定字串中未找到匹配項,則返回 null 值。換句話說,exec() 方法將字串作為引數。此字串將被檢查是否與給定字串匹配。

語法

這是 JavaScript 中正則表示式的 exec() 方法的語法:

regularExpressionObj.exec(string)

示例

以下是 JavaScript 中 exec() 方法的一個示例。這裡我們在給定字串中搜索特定單詞。

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>exec() - Regular Expression in JavaScript</title> </head> <body> <p>click to get exec() method output</p> <button onclick="findMatch()">Search</button> <p id="tutorial"></p> <script> function findMatch() { var txt ="Learning regular expressions in JavaScript"; var search1 = new RegExp("JavaScript"); var search2 = new RegExp("Abdul") var res1 = search1.exec(txt); var res2 = search2.exec(txt); document.getElementById("tutorial").innerHTML ="Given string is:"+txt +"<br>"+ "Specific word to match are:"+search1+" "+search2+"<br>"+"Result of two search keys: "+res1+" "+res2 } </script> </body> </html>

test() 方法

test() 方法在給定字串中搜索指定字串的匹配項。exec() 和 test() 方法之間的區別在於,如果在給定字串中存在匹配項,則 exec() 方法將返回指定的匹配項,如果不存在則返回 null,而 test() 方法返回布林結果,即如果存在指定的匹配項則返回 true,否則返回 false。

語法

這是 JavaScript 中 test() 方法的語法:

regularExpressionObj.test(string)

它也以給定的輸入字串作為引數,並返回布林結果。

示例

此程式也在字串中搜索給定單詞,但這裡我們使用的是 test() 方法。

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>test() - Regular Expression in JavaScript</title> </head> <body> <p>click to get exec() method output</p> <button onclick="findMatch()">Search</button> <p id="tutorial"></p> <script> function findMatch() { var txt ="Learning regular expressions in JavaScript"; var search1 = new RegExp("JavaScript"); var search2 = new RegExp("Abdul") var res1 = search1.test(txt); var res2 = search2.test(txt); document.getElementById("tutorial").innerHTML ="Given string is:"+txt+"<br>"+ "Specific word to match are:"+search1+" "+search2+"<br>"+"Result of two search keys: "+res1+" "+res2 } </script> </body> </html>

search() 和 test() 的區別

test() 方法驗證匹配項並返回布林值,而 exec() 方法捕獲組並將正則表示式與輸入匹配。如果您只需要測試輸入字串是否與正則表示式匹配,則 RegExp.test() 最合適。它將提供一個布林值,使其非常適合條件語句。

RegExp.exec() 方法提供類似陣列的返回值,其中包含所有捕獲組和匹配索引。因此,當您需要在匹配後處理捕獲組或索引時,它非常有用。

示例

在下面的示例中,我們使用這兩種方法執行相同的正則表示式。在這裡您可以觀察到 test() 方法返回布林值,而 exec() 方法返回陣列。

<!DOCTYPE html> <html> <body> <script> var regex = RegExp("^([a-z]+) ([A-Z]+)$"); document.write("Result of the exec method: "+regex.exec("hello WORLD")) document.write("<br> Result of the test method: "+regex.test("hello WORLD")) </script> </body> </html>

輸出

執行上述程式後,您將獲得以下輸出。

Result of the exec method: hello WORLD,hello,WORLD
Result of the test method: true

請注意,exec 返回的陣列中的第一個索引是完整的匹配字串。後面的索引是正則表示式捕獲的各個組。

更新於:2022年9月2日

6K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告