0

I am using JavaScript regex that will check for the URL.

(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))

I use this Regex to send messages to Twitter. What happens is that this makes sure that the URL contains WWW and/or HTTP.

But the problem is if the URL is like Guaridan.com.uk Twitter consideres it as a URL.

So how can I modify my Regex that it will not check for HTTP or WWW, meaning if it is there then it would not take any effect.

So it would match like My.co.com or dummy.com.in.

5
  • 1
    If you use jquery, why don't use use jquery's URL validation routines? Commented Jan 4, 2012 at 10:31
  • @fge i dont know anything about it. can you post some example Commented Jan 4, 2012 at 10:33
  • See here: docs.jquery.com/Plugins/Validation/Methods/url Commented Jan 4, 2012 at 10:37
  • Tested www.guardian.co.uk in the referred jQuery page, it does not validate. Commented Jan 10, 2012 at 10:16
  • @fge there is nothing in the jQuery API regarding url validation. Pehraps you are thinking about plugins Commented Nov 12, 2012 at 5:18

3 Answers 3

1

If you want to match just domain names you have to match almost any word plus ".", "_" and "-" signs. An approach may be match at least the root domains (.com,.net,.us,.co.uk,.es,.fr ... and so on) but the list will be huge. You may want to match just anything that has dot separated words, not that it is going to be a domain for sure but you might try connecting to it.

This regex: ([\d\w]+?:\/\/)?([\w\d\.\-]+)(\.\w+)(:\d{1,5})?(\/\S*)?

will match:

  • group 1 as protocol:// (optional)
  • group 2 concat group 3 as domain
  • group 3 is the top level domain
  • group 4 as :port (optional)
  • group 5 as query (optional)
Sign up to request clarification or add additional context in comments.

Comments

1

jQuery regular expression for www and http URL format.

Regular expression that accept www as well as http URL is given below:

var pattern = /(?:https?:\/\/)?(?:www\.)?(?:https?:\/\/)?(?:www\.)(?:https?:\/\/)?(?:www\.)?(\/\S*)?/;

Regular expression that accept www, http, abc.com is given below:

var pattern = ([\d\w]+?:\/\/)?([\w\d\.\-]+)(\.\w+)(:\d{1,5})?(\/\S*)?

2 Comments

Welcome to SO! Can you reformat your answer a little, putting the Regex into code brackets? Also recheck for typos, please.
its up to ur need where u using this Regex, just put urself code brackets or semicolon.like i add this Regex in my jqueryvalidation.js as "url": { "regex": /(?:https?:\/\/)?(?:www\.)?(?:https?:\/\/)?(?:www\.)(?:https?:\/\/)?(?:www\.)?(\/\S*)?/i, "alertText": "* Invalid URL" }
0

Try This...

/^http:\/\/|(www\.)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/

It's working exactly what peoples are want.

it takes with or with out http://, https://, and www.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.