0

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?

Fraser
  • 13,348
  • 6
  • 50
  • 101
user1222312
  • 63
  • 1
  • 1
  • 6

4 Answers4

2

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 ^[^=]+=

Devin Burke
  • 13,392
  • 11
  • 53
  • 80
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: ^(.*?=)

Community
  • 1
  • 1
npinti
  • 51,070
  • 5
  • 71
  • 94
2

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.

rodneyrehm
  • 13,169
  • 38
  • 56
1

This pattern should match until =:

.+?=
Sufian Latif
  • 12,648
  • 3
  • 32
  • 70