Regular expression (regular expression) NOT containing string

It has already been asked here, but the responder was satisfied with the response to 2 characters. I repeat his main question:

As a rule, is there a way, say, does not contain a string in the same way that I can say does not contain with the symbol [^ a]?

I want to create a regular expression that matches two end lines and everything, but only if no other visibility of this line is found inside. But I will be better satisfied with the general answer to the question quoted

Example:

Strings "<script>"and"</script>"

It must match

"<script> something something </script>"

but not

"<script> something <script> something something </script>"
+3
source share
3 answers

? . :

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

: ; , , ; .

+3

-

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

html. ,

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

. - .

, START, END foobar :

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

Use negative browsing . Lookarounds give zero-width matches - which means they don't consume any characters in the original 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

Syntax for negative mapping (?!REGEX). It searches REGEXand returns false if a match is found. A positive lookahead (?=REGEX)returns true if a match is found.

+1
source

Source: https://habr.com/ru/post/1734383/


All Articles