-2

I am trying to build a regex using JavaScript for url that can accepts only with https:// ( http://) or without http like the url which starts with www.

like

http://www.google.com
https://www.google.com
www.google.com

Here is my regex

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

but what wrong is here is that it some times accpet do not accept

www.abc.com

it accepts www.acbc and

 http://www.google
 https://www.google
6
  • Sorry for my poor English... I am a little confused... like what does "with or with" in title and something like "sometimes accept do not accept" mean...
    – iplus26
    Aug 3, 2015 at 8:03
  • Please don't tag-spam. This question has nothing to do with jquery, or (unless you're writing code for iOS, which the regex syntax you're using suggests you're not) nsregularexpression, or regular-language (if it applied here, it would apply to every regex question everywhere). Aug 3, 2015 at 8:03
  • sorry @iplus26 kindly check the statement again mean regex can accept the url with out http ,https in short also accepts url whcih start with www. Aug 3, 2015 at 8:10
  • 1
    Would you explain the purpose of the later part of the regular expression? [a-z0-9]+([\-\.]{1}[a-z0-9]+)+\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?
    – Churix
    Aug 3, 2015 at 8:18
  • 1
    According to W3C and RFC3986, the definition of an URL could be very broad. You have to be more precise on what you really need. Your question only ask for the urls begins with https or http or www, but never mention about what you need to check in the path.
    – Churix
    Aug 3, 2015 at 8:45

1 Answer 1

1

Try this: ((http|ftp|https)://)?([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?

Update:

If you want use it in JavaScript, it should be:

var re = /((http|ftp|https):\/\/)?([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})(:[0-9]{1,4})*(\/[a-zA-Z0-9\&%_\.\/-~-]*)?/;

console.log(re.test('http://www.google.com'));
console.log(re.test('https://www.google.com'));
console.log(re.test('www.google.com'));
console.log(re.test('google'));

The definition of url is very board, so I suppose that only the regular expression can't do all the jobs.

4
  • Same issue @iplus it accepts wwww.foo and wwwwdsddsdsds.foo and also www.goo tested on debuggex.com Aug 3, 2015 at 8:27
  • 2
    @usama if you want xxxx.foo not accepted, I suppose that you have to list all '.com', '.cn', '.org'... which is impossible... and neither should you refuse url like 'photos.google.com' if it's not necessary?
    – iplus26
    Aug 3, 2015 at 8:40
  • i am using your regex like this /((http|ftp|https)://)?([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?/, but it is giving me this error Uncaught SyntaxError: Unexpected token ) Aug 3, 2015 at 9:03
  • @usama you should add '\' before '/' when you use it in JavaScript. It's my fault that I just post the normal regular expression here.
    – iplus26
    Aug 3, 2015 at 9:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.