Skip to content Skip to sidebar Skip to footer

Extract Url From String With Javascript

this sounds like something you could just google, but been looking for hours. basically have this string i am ajaxing from another site 'function onclick(event) { toFacebook('http:

Solution 1:

if it is always with the dash (i'm assuming you want everything before the dash), you can use the split method:

var arr = split(val);

your data will be in arr[0]

Solution 2:

you can use these functions in 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:""};
}
}

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

Solution 3:

just ended up doing this

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

regular expressions are beyond me.

Solution 4:

yourFunction.toString().split("'")[1] does the job.

Solution 5:

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

    var testUrl = text.match(urlRegex);

    if (testUrl === null) {

    return"";

      }else {

    var urlImg = testUrl[0];

    return urlImg;

     }

Post a Comment for "Extract Url From String With Javascript"