Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从字符串java脚本获取特定文本_Javascript_Jquery_String - Fatal编程技术网

Javascript 从字符串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

我正在使用java脚本中的字符串,例如,如果我有以下字符串:

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]