源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>教程</title> </head> <body> <script> var str = 'runoob'; var patt1 = new RegExp('\w', 'g'); // 有轉義作為正則表達式處理 var patt2 = new RegExp('w', 'g'); // 無轉義作為字符串處理 var patt3 =/w+/g; // 與 patt1 傚果相同 document.write(patt1.test(str)) //輸出 true document.write("<br>") document.write(patt2.test(str)) //輸出 false document.write("<br>") document.write(patt3.test(str)) //輸出 true </script> </body> </html>
運行結果