1

I want to find anything that comes after s= and before & or the end of the string. For example, if the string is

t=qwerty&s=hello&p=3

I want to get hello. And if the string is

t=qwerty&s=hello

I also want to get hello

Thank you!

dmr
  • 21,811
  • 37
  • 100
  • 138
  • are you parsing query strings... with regexen? – sehe Dec 15 '11 at 22:15
  • @sehe: No, it's not a query string. It's a custom html attribute that stores part of a query string. – dmr Dec 15 '11 at 22:17
  • isn't that largely the same deal? After you strip the non-querystring-y bits, it will all just be querystring-y. Text is only text, and if you choose the formats wisely, you have less work to do – sehe Dec 15 '11 at 22:20

4 Answers4

5

\bs=([^&]+) and grabbing $1should be good enough, no?

edit: added word anchor! Otherwise it would also match for herpies, dongles...

fge
  • 119,121
  • 33
  • 254
  • 329
1

Why don't you try something that was generically aimed at parsing query strings? That way, you can assume you won't run into the obvious next hurdle while reinventing the wheel.

jQuery has the query object for that (see JavaScript query string)

Or you can google a bit:

function getQuerystring(key, default_)
{
   if (default_==null) default_=""; 
   key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
   var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
   var qs = regex.exec(window.location.href);
   if(qs == null)
     return default_;
   else
     return qs[1];
}

looks useful; for example with

http://www.bloggingdeveloper.com?author=bloggingdeveloper

you want to get the "author" querystring's value:

var author_value = getQuerystring('author');
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
1

The simplest way to do this is with a selector s=([^&]*)&. The inside of the parentheses has [^&] to prevent it from grabbing hello&p=3 of there were another field after p.

gereeter
  • 4,731
  • 1
  • 28
  • 28
0

You can also use the following expression, based on the solution provided here, which finds all characters between the two given strings:

(?<=s=)(.*)(?=&)

In your case you may need to slightly modify it to account for the "end of the string" option (there are several ways to do it, especially when you can use simple code manipulations such as manually adding a & character to the end of the string before running the regex).

Dean Gurvitz
  • 854
  • 1
  • 10
  • 24