4

(appendCsrfToken).+(\.do\?\w)
matches on a String like...
document.forms[0].action = appendCsrfToken("search.do?lname=Smith");

What I would like to do is find Strings that have the .do? portion but don't have the appendCsrfToken portion. For instance...
document.forms[0].action = "search.do?lname=Smith";

I thought that the following would negation would work but I'm getting no matches when I test it
(^appendCsrfToken).+(\.do\?\w)

How do I properly negate the appendCsrfToken to get the match I'm looking for?

2 Answers 2

4

Using ^ for negation only works in character classes. You need a lookahead. The easiest way is to look from the beginning of the string (^) all the way through for appendCsrfToken with a negated lookahead. If that works, then go ahead and match the do?:

^(?!.*appendCsrfToken).*(\.do\?\w)

Demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes indeed - that does the trick - what I don't quite understand is the ^(?! portion. Does the article you link explain this (I haven't yet read it)?
@ZackMacomber yes, that's why I linked it ^^. The ^ and the (?!...) are unrelated. ^ (outside of a character class) is an anchor that matches only at the beginning of the string (without consuming any characters). The (?!...) is a negative lookahead, which fails if its subpattern matches - but doesn't actually consume what's matched by the subpattern... hence it looks ahead. But the article explains this much better than I ever could.
0

You can also use negative lookbehind. Here's the solution

^(?<!.*appendCsrfToken).*(\\.do\\?\\w)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.