method 1
var str = "this is a test";
var re = new RegExp("is");
var m = re.exec(str);
if(m) alert(m[1]);
var re = new RegExp("is");
var m = re.exec(str);
if(m) alert(m[1]);
method 2
var str = "this is a test";
var re=/test/gi;
alert(str.replace(regex,"!!!"));
var re=/test/gi;
alert(str.replace(regex,"!!!"));
NOTE: RegExp Object now supports search(), match(), replace() and split() in String Object. For more detail please reference the hyperlinks.
Reference:
JavaScript RegExp Object Reference
石頭閒語 - Regular Expression (RegExp) in JavaScript
Regular Expression在Javscript下的兩種作法
沒有留言:
張貼留言