2009年3月4日 星期三

JavaScript Regular Expression

Regular Expression in JavaScript
method 1
var str = "this is a test";
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,"!!!"));


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下的兩種作法

沒有留言:

Powered By Blogger