Extract url from string with javascript

it sounds like something you could just google but look for a watch.

basically there is this line, I'm ajaxing from another site

'function onclick(event) { toFacebook("http://www.domain.com.au/deal/url-test?2049361208?226781981"); }'

this is because im is extracting onclick.

I just want to extract url from string.

Any help would be greatly appreciated.

---- edit ----

OK IF I GO HERE. http://regexlib.com/RESilverlight.aspx regext online tester

and run this regular expression.

(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

in my line, it perfectly emphasizes url. Can I just get it to work with JS?

+3
source share
6 answers

if it is always with a dash (I assume you want everything before the dash), you can use the split method:

var arr = split(val);

your data will be in arr [0]

+3
source

javascript:

function parseUrl1(data) {
var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;

if (data.match(e)) {
    return  {url: RegExp['$&'],
            protocol: RegExp.$2,
            host:RegExp.$3,
            path:RegExp.$4,
            file:RegExp.$6,
            hash:RegExp.$7};
}
else {
    return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
}
}

function parseUrl2(data) {
var e=/((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?/;

if (data.match(e)) {
    return  {url: RegExp['$&'],
            protocol: RegExp.$2,
            host:RegExp.$3,
            path:RegExp.$4,
            file:RegExp.$6,
            hash:RegExp.$7};
}
else {
    return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
}
}

: http://lawrence.ecorp.net/inet/samples/regexp-parse.php

0

just finished this

$a.substring($a.indexOf("http:"), $a.indexOf("?"))

regular expressions are outside of me.

0
source

yourFunction.toString().split("'")[1] performs the task.

0
source
 var urlRegex = /(http?:\/\/[^\s]+)/g;

    var testUrl = text.match(urlRegex);

    if (testUrl === null) {

    return "";

      }else {

    var urlImg = testUrl[0];

    return urlImg;

     }
0
source

Edit: Here is another solution that will retrieve the URL:

var function = 'function onclick(event) { toFacebook("http://www.domain.com.au/deal/url-test?2049361208?226781981"); }'
function.match(/(http:[^"]+)/)[0]

Old answer: Use regular expressions:

javascript regex to extract anchor text and url from anchor tags

var url_match = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;

alert(url_match.test("http://stackoverflow.com"));
-2
source

Source: https://habr.com/ru/post/1771923/


All Articles