2010年12月6日 星期一

Sub-pattern Matching in Javascript


I am getting:


result[0] = "<eval>content1</eval>";
result[1] = "<eval>content2</eval>";


I was expecting


result[0] = "content1";
result[1] = "content2";


any suggestion?




Simplest solution is to use the exec method on the RegExp object. e.g.


var str = "<eval>content1</eval>\n<eval>content2</eval>";


var pattern = /<.+>(.*)<\/.+>/mg;


var match;



while ((match = pattern.exec(str)))


{
alert(match[1]);
}


 


 


http://markmail.org/message/tlorcsxipkhmjo4z#query:+page:1+mid:w44kinhyc2fs4y2y+state:results


沒有留言:

張貼留言