Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I parse this string on a javascript,

var string = "http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1";

I just want to get the "265956512115091" on the string. I somehow parse this string but, still not enough to get what I wanted.

my code:

var newstring = string.match(/set=[^ ]+/)[0]; 

returns:

a.265956512115091.68575.100001022542275&type=1
share|improve this question
    
To properly parse this, you should first dissect it into its parts (e,g, the key-values of the query string), then URL-decode the key and value, and only then extract the data you're interested in from the value with the key you're interested in. –  Lucero Jan 28 '12 at 13:40

2 Answers 2

up vote 5 down vote accepted
try this : 

  var g=string.match(/set=[a-z]\.([^.]+)/);

g[1] will have the value

http://jsbin.com/anuhog/edit#source

share|improve this answer
    
Nyc, want this. Thanks! –  user987361 Jan 28 '12 at 13:39

You could use split() to modify your code like this:

var newstring = string.match(/set=[^ ]+/)[0].split(".")[1]; 

For a more generic approach to parsing query strings see:

Parse query string in JavaScript

Using the example illustrated there, you would do the following:

var newstring = getQueryVariable("set").split(".")[1];

share|improve this answer
    
why dont you use groups ? –  Royi Namir Jan 28 '12 at 13:33
    
Tnx. It worked! –  user987361 Jan 28 '12 at 13:38
    
My first instinct was to modify the regex too, but I thought the more general notion of parsing a query string was helpful. Braveyard's answer answer (linked above) is based on split() and I just carried forward in kind. –  biscuit314 Jan 28 '12 at 13:39
    
If you want something generic, then by all means do apply proper URL decoding! –  Lucero Jan 28 '12 at 13:54
    
You are right, of course! –  biscuit314 Jan 28 '12 at 14:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.