I'm trying to build regex that will check specific route for if there is "?emial=true" query attached. It should only match positively if there is no "?emial=true" in query. I'm trying to use Negative lookahead assertion but when I paste example below and my regex into tool like https://regexr.com/ then it still shows as match. Somehow last character is not counted in ( 'x' in example below).
((\/some-path\/some-path)(-*[\da-zA-Z]*)*)(?!(\?email=true))
/some-path/some-path-1a-dwadsd-waeas-wasdx?email=true
Can someone help me with this regex? I feel like I am close but can't seem to figure it out :(
CodePudding user response:
There is already APIs to get search parameters, you should not write regex for it.
let path = "/some-path/some-path-1a-dwadsd-waeas-wasdx?email=true"
let result = new URL(path,location).searchParams.get('email') == "true"
console.log(result)
CodePudding user response:
You can capture the quantified part inside a positive lookahead, and then use a backreference to the group value in the consuming pattern part to avoid backtracking:
\/some-path\/some-path(?=([-\da-zA-Z]*))\1(?!\?email=true)
^^^^^^^^^^^^^^^^^^^^
See the regex demo.