3

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://www.domain.com.au/deal/url-test?2049361208?226781981"); }'

it comes out like that because im extracting the onclick.

i just want to extract the url from the string.

any help would be greatly appreciated.

---- edit ----

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

and run this regex.

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

on my string, it highlights the url perfectly.. i just can get it to run with JS?

Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
Alessandro
  • 305
  • 2
  • 8
  • 22

6 Answers6

3

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]

Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
0

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

Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
0

just ended up doing this

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

regular expressions are beyond me.

Alessandro
  • 305
  • 2
  • 8
  • 22
0
 var urlRegex = /(http?:\/\/[^\s]+)/g;

    var testUrl = text.match(urlRegex);

    if (testUrl === null) {

    return "";

      }else {

    var urlImg = testUrl[0];

    return urlImg;

     }
Kathir
  • 4,359
  • 3
  • 17
  • 29
0

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

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
Kaj Dijkstra
  • 327
  • 1
  • 4
  • 14
-3

Edit: Here is another solution that will extract 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"));
Community
  • 1
  • 1
Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58