如果没有www,正则表达式无效

时间:2016-06-10 05:33:59

标签: javascript jquery regex

正常表达式下面没有www

 /((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)/i

我检查过以下网址

http://regexr.com       (working, match)
http://www.regexr.com/  (working, match)
www.regexr.com          (working, match)
regexr.com              (not working, should be match)
abc@gmail.com           (working, not match)

如果我使用下面的代码

((?:https?\:\/\/|)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)

然后它也匹配电子邮件ID

abc@gmail.com           (not working, should not match)

2 个答案:

答案 0 :(得分:1)

Demo on regexr.com

(https?:\/\/)?\w+(\.\w+)+(\/\w+)*(\/\w+\.\w+)?(\?[\w%&=.]*)*(?=[^\w.?&%=])

试试这个,

答案 1 :(得分:0)

正则表达式正常,更多信息click here

^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$

工作,因为它匹配以下Url'而不是电子邮件ID

http://regexr.com/

http://www.regexr.com/

https://regexr.com

https://www.regexr.com

www.regexr.com

regexr.com

abc@gmail.com(工作,与电子邮件ID不匹配)

Demo