1

RegExp gurus, heed my call!

This is probably super simple, but I've painted myself in a mental corner.

Taking a regular URL, split after the ?, which gives a string like variable=val&interesting=something&notinteresting=somethingelse I want to extract the value of interesting.

The name of the variable I'm interested in can be a substring of another variable. So the match should be

  • either beginning of string or "&" character
  • followed by "interesting="
  • followed by the string I want to capture
  • followed by either another "&" or end of string

I tried something along the lines of [\^&]interesting=(.*)[&$] but I got nothing...

Update This is to be run in a Firefox addon on every get request, meaning that jQuery is not available and if possible I would like to avoid the extra string manipulation caused by writing a function.

To me this feels like a generic "extract part of a string with regex" but maybe I'm wrong (RegEx clearly isn't my strong side)

danneth
  • 2,721
  • 1
  • 23
  • 31
  • Don't reinvent wheels, use a library: http://blog.stevenlevithan.com/archives/parseuri – georg Mar 19 '13 at 10:57
  • Not made clear in the original question, but this is to be used inside a Firefox add-on (because I was really thinking there would be a pure RegEx solution), and due to various internal reasons I would really like to avoid writing a funciton to extract this for efficiency reasons – danneth Mar 19 '13 at 11:57

3 Answers3

1

simple solution

var arr = "variable=val&interesting=something&notinteresting=somethingelse".split("&");
for(i in arr) {
var splits = arr[i].split("=");
if(splits[0]=="interesting") alert(splits[1]);
}

also single line match

"variable=val&interesting=something&notinteresting=somethingelse".match(/(?:[&]|^)interesting=((?:[^&]|$)+)/)[1]
Pankaj Phartiyal
  • 1,691
  • 1
  • 12
  • 23
  • The single line match does not work if I change the order of `interesting` and `notinteresting` then it gives the value `somethingelse` instead of the desired `something`. But this is close to what I was looking for – danneth Mar 19 '13 at 12:02
  • edited regex :) it should work now – Pankaj Phartiyal Mar 19 '13 at 12:59
  • Excellent! Tested with the desired field in the beginning, middle, and end of the string and all works. Thank you. – danneth Mar 19 '13 at 13:12
0
function getValue(query)
{
var obj=location.search.slice(1),
    array=obj.split('&'),
    len=array.length;
    for(var k=0;k<len;k++)
    {
        var elm=array[k].split('=');
        if(elm[0]==query)return elm[1];
    }

}

This function directly extract the query URL and return the corresponding value if present.

//usage
var get=getValue('interesting');
console.log(get);//something
spaceman12
  • 1,039
  • 11
  • 18
0

If you're using the Add-on SDK for Firefox, you can use the url module:

https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/url.html

This is much better than using regex.

therealjeffg
  • 5,790
  • 1
  • 23
  • 24