如果我有一个字符串,
currentUrl = "+http://localhost:30754/api/Index/PostStatus/+"
使用 "+" 拆分该字符串,
currentUrl.split("+")
输出:
["", "http://localhost:60742/api/filemover/PostStatus/", ""]
0: ""
1: "http://localhost:60742/api/filemover/PostStatus/"
2: ""
length: 3
现在我想找到“ http://localhost:60742/api/filemover/PostStatus/ ”的网址
那我怎么能用jQuery找到
注意:
我不想使用这种静态方式,想要像搜索词“http”一样动态获取,我可以获取路径或完整字符串
currentUrl.split("+")[1]
谢谢
首先,JQuery 的目的是操作 DOM,这意味着它没有以任何方式操作字符串的函数。在另一个字符串中动态获取某个字符串的最佳方法是使用正则表达式
因为您想使用 javascript获取包含在+符号之间的 URL,所以您可以像这样执行
var chaine = "+http://localhost:30754/api/Index/PostStatus/+";
// The pattern which I provide as parameter to the match method will capture everything between + symble
var regexResult = chaine.match(/\+(.*)\+/);
// the regexResult will be equal to the array provide bellow
/*
Array [
"+http://localhost:30754/api/Index/PostStatus/+",
"http://localhost:30754/api/Index/PostStatus/"
]
*/
// because there are a match the URL will be accessible as the element at the index 1 of the array
var url = regexResult[1];
console.log(url);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句