这是我的变量,我只想从中提取 URL,例如在本例中我希望最终得到结果: http://url.com/index.php/foo/bar/new/key/34941cd947592adb1594b6f649468a33/
var variable = "function onclick(event) { setLocation('http://url.com/index.php/foo/bar/new/key/34941cd947592adb1594b6f649468a33/')}"
最佳答案
使用这个:
var testUrl = variable.match(/'(http:[^\s]+)'/),
onlyUrl = testUrl && testUrl[1];
onlyUrl
变量包含提取的 URL 或 null
。
编辑:
之前只是对OP的回答。更新后处理 https:
var testUrl = variable.match(/'(https?:[^\s]+)'/),
onlyUrl = testUrl && testUrl[1];
关于javascript - 从字符串中提取 URL javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18648951/