Amir
Amir

Reputation: 4770

RegExr to match URL var up to & or #

So I have a URL like this:

http://www.someurl.com/subdir/dir/name?urlVar=true#someotherpath/td/a/way/p/data/1612040?menu=menu1&test=mytest&test=two

I'm trying to write a regExr to get my urlVar, but I've only gotten as far as this: [!?&]urlVar=([^&#]+)(&|$)

If I remove the # in the expression above I get this:

urlVar=true#someotherpath/td/a/way/p/data/1612040?menu=menu1&

But I want it to stop at the pound sign. Adding the pound in the [^&] capture group doesn't seem to work. Any ideas?

Upvotes: 0

Views: 88

Answers (3)

Mariano
Mariano

Reputation: 6511

This is not matching in your regex:

[!?&]urlVar=([^&#]+)(&|$)
                    ^^^^^
  • It's not followed by a "&" nor the end of string.

Simply remove that construct and it will work:

[!?&]urlVar=([^&#]+)

Upvotes: 0

Elvedin Hamzagic
Elvedin Hamzagic

Reputation: 855

[^?&]+\?urlVar=([^&#]+)(?:&|#)?.*


[^?&]+\? - should read any character which is not ? or & up to ?.

([^&#]+) - should capture value.

(?:&|#)?.* - should read fallowing & or #, if exists (but not capture that), and any character after.


But I would rather replace second group with character class: [^?&]+\?urlVar=([^&#]+)[&#]?.*

You can validate it here or here.

Upvotes: 1

ndnenkov
ndnenkov

Reputation: 36100

Use lookahead:

urlVar=[^#]+(?=#|$)

What this means is match urlVar=, followed by multiple characters that are not # and end the match when you reach a # character or the end of the string.

See it in action


If you use a non-javascript regex engine, it probably supports lookbehinds too.

Therefore, you can extract only the true part without the urlVar=:

(?<=urlVar=)[^#]+(?=#|$)

Upvotes: 1

Related Questions