JavaScript 的正則表達式由 RegExp 物件表示,它提供了 test()、exec() 與 compile() 三個方法。
test() 用來檢索字串中是否含有符合的內容,回傳值是 true 或 false。
var patt1 = new RegExp("e");
document.write(patt1.test("The best things in life are free"));
exec() 同樣用於檢索字串中的指定值,但回傳的是被找到的內容;若沒有符合的結果,則回傳 null。
var patt1 = new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
(本篇先整理到此,後續再補充 compile() 的用法。)