Javascript 在javascript中解析字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9045548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse string in javascript
提问by Robin Carlo Catacutan
How can I parse this string on a javascript,
我怎样才能在 javascript 上解析这个字符串,
var string = "http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1";
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.
我只想在字符串上获取“ 265956512115091”。我以某种方式解析了这个字符串,但仍然不足以得到我想要的。
my code:
我的代码:
var newstring = string.match(/set=[^ ]+/)[0];
returns:
返回:
a.265956512115091.68575.100001022542275&type=1
采纳答案by Royi Namir
try this :
var g=string.match(/set=[a-z]\.([^.]+)/);
g[1] will have the value
回答by biscuit314
You could use split()
to modify your code like this:
您可以split()
像这样修改您的代码:
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];
var newstring = getQueryVariable("set").split(".")[1];