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. :)
精彩评论