最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Parse string in javascript - Stack Overflow

programmeradmin2浏览0评论

How can I parse this string on a javascript,

var string = ".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

How can I parse this string on a javascript,

var string = "http://www.facebook./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 asked Jan 28, 2012 at 13:21 Robin Carlo CatacutanRobin Carlo Catacutan 13.7k12 gold badges54 silver badges86 bronze badges 1
  • 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 Commented Jan 28, 2012 at 13:40
Add a ment  | 

3 Answers 3

Reset to default 5
try this : 

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

g[1] will have the value

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

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];

You can use capturing group in the regex.

const str = 'http://www.facebook./photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1';

console.log(/&set=(.*)&/.exec(str)[1]);

发布评论

评论列表(0)

  1. 暂无评论