5

Here is my variable from which I want to extract only URL, for example in this case I want at the end the result: 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/')}"
4

1 回答 1

12

用这个:

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];
于 2013-09-06T02:38:52.177 回答