开发者

Why isnt this regex picking up my pattern? (works on flash)

Here is my example http:/开发者_开发问答/jsfiddle.net/mwDCV/1/

Using http://gskinner.com/RegExr/ i get exactly what i want however in javascript i do not

pattern

pass(word)?\=\s*(\<[^>]+\>)*\s*([^<]+)
aka
var myRe = new RegExp('pass(word)?\=\s*(\<[^>]+\>)*\s*([^<]+)', 'gim');

text

<br />
Run
password=
<br />
test
<br />


EDIT: Sorry, I was incorrect about the reason.

The real reason is that in javascript you need to escape your escape slashes like so:

from: var myRe = new RegExp('pass(word)?\=\s*(\<[^>]+\>)*\s*([^<]+)', 'gim');

to: var myRe = new RegExp('pass(word)?=\\s*(<[^>]+>)*\\s*([^<]+)', 'gim');

in order for the \s to come out properly.

You can do console.log(myRe) and see how the browser parsed your expression (with missing \'s).

Seen here: jsfiddle (old, using / / designation)

But parsing html with regex is frowned upon, you might be better off grabbing the text nodes of wherever this element is and parse that instead (strip the newlines and test for password=.+?)


You use the RegExp constructor which takes a string, so your backslashes were string escapes. Use the regexp literal instead, as in the other answer. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜