Javascript 从字符串java脚本获取特定文本
我正在使用java脚本中的字符串,例如,如果我有以下字符串:Javascript 从字符串java脚本获取特定文本,javascript,jquery,string,Javascript,Jquery,String,我正在使用java脚本中的字符串,例如,如果我有以下字符串: str = "miniprofile-container http://www.linkedin.com/miniprofile?vieweeID=8573&context=anet&view" 我想要这类字符串中的ID号“8573”。它们的长度不同,但结构与上述相同。所以请帮助我实现它 我可以使用strcpy函数,但它不是通用的方法 var str = "miniprofile-container http://w
str = "miniprofile-container http://www.linkedin.com/miniprofile?vieweeID=8573&context=anet&view"
我想要这类字符串中的ID号“8573”。它们的长度不同,但结构与上述相同。所以请帮助我实现它
我可以使用strcpy函数,但它不是通用的方法
var str = "miniprofile-container http://www.linkedin.com/miniprofile?vieweeID=8573&context=anet&view";
var pattern = /[0-9]+/g;
var matches = str.match(pattern); //8573
演示:
演示:如果您不想/不需要使用正则表达式,也可以尝试以下操作:
var str = "miniprofile-container http://www.linkedin.com/miniprofile?vieweeID=8573&context=anet&view";
var s1 = str.indexOf("vieweeID=")+"vieweeID=".length;
var matches = str.substring(s1,str.indexOf("&context"))
alert(matches);
如果您不想/不需要使用正则表达式,也可以尝试以下方法:
var str = "miniprofile-container http://www.linkedin.com/miniprofile?vieweeID=8573&context=anet&view";
var s1 = str.indexOf("vieweeID=")+"vieweeID=".length;
var matches = str.substring(s1,str.indexOf("&context"))
alert(matches);
str.match(/vieweeID\=(.*?)\&/)[1]