I have string like this: http://someurl.com?someparameter=dhws6sd6cvg and i would like to mach part from first letter to '=' with javascript regex. Is it possible?
- 13,348
- 6
- 50
- 101
- 63
- 1
- 1
- 6
4 Answers
The use of .* will cause the regex to back track and use of () will create a backreference. So we can use this to capture the given string ^[^=]+=
- 13,392
- 11
- 53
- 80
- 42
- 2
You can create a group which will match all the words starting from the beginning till the =. You can then access the regex group as shown here.
Something like: ^(.*?)= Should yield the following group value: http://someurl.com?someparameter. If you would like to include the =, then, just do as follows: ^(.*?=)
Up front: If you're parsing URLs, you might want to use one of the existing URL parsers or URI.js.
The beginning of a string is denoted by ^ so your RegEx could look like ^([^=]+). This matches everything but = from the beginning of the string. If you only want the part after the ?, try \?([^=]+). Note that you'll only get the very first parameter this way.
Have a look at MDN explaining RegExp.
- 13,169
- 38
- 56