Reputation: 4770
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
Reputation: 6511
This is not matching in your regex:
[!?&]urlVar=([^&#]+)(&|$)
^^^^^
&
" nor the end of string.Simply remove that construct and it will work:
[!?&]urlVar=([^&#]+)
Upvotes: 0
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
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.
Therefore, you can extract only the true part without the urlVar=
:
(?<=urlVar=)[^#]+(?=#|$)
Upvotes: 1