开发者

regex (regular expressions) pattern NOT containing a string

It was already asked here, but the asker got satisfied with a 2 ch开发者_开发百科aracter finding answer. I repeat his basic question:

Generally, is there any way, how to say not contains string in the same way that I can say not contains character with [^a]?

I want to create a regexp that matches two ending strings and everything between, but only if no other occurance of a given string is found inside. But I will be satisfied best with the general answer to the quoted question

Example:

The strings are "<script>"and"</script>"

It should match

"<script> something something </script>"

but not

"<script> something <script> something something </script>"


Did you read my answer to that question? It gives a more general solution. In your case it would look like this:

(?s)<script>(?:(?!</?script>).)*</script>

In other words: match the opening sequence; then match one character at a time, after ensuring that it's not the beginning of the closing sequence; then match the closing sequence.


Use negative lookahead. Lookarounds give zero width matches - meaning that they don't consume any characters in the source string.

var s1 = "some long string with the CENSORED word";
var s2 = "some long string without that word";
console.log(s1.match(/^(?!.*CENSORED).*$/));//no match
console.log(s2.match(/^(?!.*CENSORED).*$/));//matches the whole string

The syntax for negative lookahead is (?!REGEX). It searches for the REGEX and returns false if a match is found. Positive lookahead (?=REGEX) returns true if a match is found.


The correct expression for your problem is

"^<script>((?!<script>).)*</script>$"

This shouldn't be used for html manipulation. This doesn't address cases like

<script> foo <script type="javascript"> bar </script>

and many others. A parser is the correct solution here.

The more general expression for matching strings beginning with START, ending with END without the specific character sequence foobar in-between is:

"^START((?!foobar).)*END$"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜