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/')}"
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With