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];
Share:
13,400
Admin
Author by

Admin

Updated on June 11, 2022

Comments