Extract URL from string javascript
13,400
Use this:
var testUrl = variable.match(/'(http:[^\s]+)'/),
onlyUrl = testUrl && testUrl[1];
The onlyUrl
variable contains the extracted URL or null
.
EDIT:
Previous was just answer to OP. Following an update to take care of https:
var testUrl = variable.match(/'(https?:[^\s]+)'/),
onlyUrl = testUrl && testUrl[1];
Author by
Admin
Updated on June 11, 2022Comments
-
Admin almost 2 years
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/')}"