开发者

Using negative lookahead multiple times (or matching multiple characters with ^)?

I want to do something like this:

/<script[^>]*>(?!<\/script>)*<\/script>/g

to match all scripts tag in a html string using javascript.

I know this won't work but i can't seem to find any other solutions. The script-tag can either use the src attribute and close it self right after (<script src="..." type="text/javascript"></script>) or can contain the code within the scrip开发者_开发知识库t-tag (<script type="text/javascript">...</script>)


You were close

/<script[^>]*>(?:(?!<\/script>).)*<\/script>/g

You must have something to eat the actual script body. That's what the . does here.

The look-ahead check must occur before every character, so it is wrapped in an extra (non-capturing) group. To capture the script source code in group 1, just add another set of parens around the (?:...) like @AlanMoore pointed out in the comments.


Try this

/<script[^>]*>.*?<\/script>/g

I don't see a reason for a negative look ahead. .*? is a lazy match so that it only matches till the next closing tag and not till the last one.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜