Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract URL from string javascript [duplicate]

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/')}"
like image 239
sh00pac Avatar asked Sep 06 '13 02:09

sh00pac


1 Answers

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];
like image 112
1111161171159459134 Avatar answered Oct 19 '22 04:10

1111161171159459134