-2

I have string like this: http://someurl.com?test&lettersg and I would like to mach part from first letter to & (without &, this part only: http://someurl.com?test).

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user1222312
  • 63
  • 1
  • 1
  • 6
  • 3
    What have you tried? This seems to be a very straightforward task which does not even require regular expressions. – Felix Kling Feb 26 '12 at 13:53
  • possible duplicate of [how to grab substring before a specified character jquery or javascript](http://stackoverflow.com/questions/9133102/how-to-grab-substring-before-a-specified-character-jquery-or-javascript) – Felix Kling Feb 26 '12 at 13:55
  • 1
    Should be easy if you've solved your previous problem: http://stackoverflow.com/questions/9392940/how-to-create-regex-from-any-letter-to-a-specific-character/9393022#9393022 ... an almost-exact copy. – Sufian Latif Feb 26 '12 at 13:57

3 Answers3

0

If its the code you want, look at the source of this JQuery Plugin (URL Parser). Or you can just go ahead and use the plugin.

robasta
  • 4,621
  • 5
  • 35
  • 53
0
var match = /^[^&]*/.exec("http://someurl.com?test&lettersg")[0];
beerbajay
  • 19,652
  • 6
  • 58
  • 75
0

For URL manipulation I suggest using URI.js.

Your problem can be solved with and without RegExp:

var string = "http://someurl.com?test&lettersg";
console.log(string.match(/^[^&]+/)[0]);
console.log(string.substring(0, string.indexOf('&')));
rodneyrehm
  • 13,442
  • 1
  • 40
  • 56