-2

I have a string like this

?i_am=busy&need=fun&s=

The busy and fun parts can change to be strings with dashes. I'd like to extract these two into variables to use with jquery. What I need is a regex that will extract them, but my regex knowledge is limited at best.

Any help is appreciated.

Tim Pierce
  • 4,996
  • 1
  • 13
  • 30
dingo_d
  • 9,839
  • 10
  • 62
  • 102
  • 3
    Aren't those URL parameters? If so you'd probably be better off using a dedicated parser. – Biffen Mar 10 '15 at 13:54
  • Please consider bookmarking the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496) for future reference. As @Biffen said, this is likely not a good candidate for regular expressions. – aliteralmind Mar 10 '15 at 13:56
  • Yeah, I'm getting them via `location.search` – dingo_d Mar 10 '15 at 13:57
  • Why it's not good candidate for regular expression? That part of the url depends on the user selection, and that selection is controlled by me (I have set up what values those strings can be, and they won't be changed). I'm just looking for the simplest way to do this, and regex came to mind. – dingo_d Mar 10 '15 at 13:59
  • 2
    URLs and query strings have a very precise syntax, and trying to use regex matching to process them tends to result in oversimplifying the problem and misinterpreting the URL in subtle ways. – Tim Pierce Mar 10 '15 at 14:02
  • Yeah, but I just need those strings (there are fixed number of them) to store in a variables. They won't change. Can I try with `indexOf` or similar? – dingo_d Mar 10 '15 at 14:04
  • In the end I pasted them to `data-iam` in a div via `$_GET['i_am']`, and the same with the second variable, and then got the data using .data('iam'). Simpler then regex. – dingo_d Mar 12 '15 at 11:19

3 Answers3

1

This will convert your URL parameters into an object:

<script type="text/javascript">
   var sourceString = "?i_am=busy&need=fun&s=";
   sourceString     = sourceString.replace(/^\?/, "");   // remove the ?
   var keyvals      = sourceString.split("&");
   var vars         = {};
   var i, keyval;
   for (i=0; i<keyvals.length; i++) {
       keyval = keyvals[i].split("=");
       vars[keyval[0]] = keyval[1];
   }
</script>
livefree75
  • 592
  • 6
  • 16
0

Try below ... you'll get two named matches. If your tool has problems with \w you may try [A-Za-z0-9_] instead of [\w] In Notepad++ you'll reference the matches like so: x-token=\1, y-token=\2 Other tools may allow to use the names like this: x-token=${x}, y-token=${y}

\?i_am=(?<x>[\w]+)&need=(?<y>[\w]+)&s=
Dlanod Kcud
  • 489
  • 4
  • 12
-1

Try this:

<script type="text/javascript">
 var re = /=(.*?)&/;
var sourcestring = "?i_am=busy&need=fun&s=";
var results = [];
var i = 0;
for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
results[i] = matches;
for (var j=0; j<matches.length; j++) {
  alert("results["+i+"]["+j+"] = " + results[i][j]);
}
i++;
}
</script>

This will helpful for you.

Yash
  • 1,427
  • 1
  • 11
  • 26