
JavaScript RegExp 對象
exec() 方法用于檢索字元串中的正規表達式的比對。
如果字元串中有比對的值傳回該比對值,否則傳回 null。
RegExpObject.exec(string)
參數
描述
string
Required. The string to be searched
所有主要浏覽器都支援 exec() 方法
在字元串中全局搜尋 "Hello" 和 "RUNOOB" 字元串:
var str="Hello world!";
//查找"Hello"
var patt=/Hello/g;
var result=patt.exec(str);
document.write("傳回值: " + result);
//查找 "RUNOOB"
patt=/RUNOOB/g;
result=patt.exec(str);
document.write("<br>傳回值: " + result);
以上執行個體輸出結果:
Returned value: Hello
Returned value: null
