views:

45

answers:

1

Hello,

Currently i have a input box which will detect the url and parse the data.

So right now, i am using

        var urlR = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)
(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
        var url= content.match(urlR);

problem is, when i enter url like www.google.com, its not working, when i entered http://www.google.com, is working..

i am not very fluent in regex. can anyone help me?

thanks.

+1  A: 

Use this

Here is a sample

var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
 var regex = new RegExp(expression);
var t = 'www.google.com';
  if (t.match(regex) )
 {
   alert("Successful match");
 } else {
   alert("No match");
 }
Daveo
For got to mention use this site http://gskinner.com/RegExr/ to test Regex and view common samples
Daveo
var urlRegex = /(https?://)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?$/; Is it like this? not working either.
bigbob
Look here http://regexr.com?2s81g you can see in the sample text that matches the URL regex are highlighted in blue
Daveo
how do i use in javascritp context? when i add this in website.. javascript not working anymore.. i think they are error
bigbob
I have updated my orginal answer to show full JavaScript example of the regex. I also changed the regex slightly
Daveo