简体   繁体   中英

Matching regex href=javascript

I have regex that matches href='javascript:...' or href="javascript:..." , but I also want to match a href=javascript:... without quotes.

How can I do it?

My regex: /\\bhref\\s*=[^"']*(?:"([^"]*)"|'([^']*)')/gi

Strings that I want to match:

<a href='javascript:alert(1)' sometags>click me</a>
<a href="javascript:alert(1)" sometags>click me</a>
<a href=javascript:alert(1) sometags>click me</a>

Sample of code:

while((match = reg.exec(s)) !== null) {
    // In each cases I want to get javascript:alert(1) in match
}

Add another case to your alternative. If there's no quote, it matches until the next whitespace or > .

/\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]*))/gi

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM