javascript - Regular Expression to match consecutive -. or .- (hypen and dot) in a string -


i have scenario consecutive -. or .- should not allowed in url.

eg. https://www.test.-nic or https://www.test-.nic whereas https://www.test.nic--xn/ should allowed

can me improve regular expression?

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

you can use negative lookahead:

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

(?=.*?(?:\.-|-\.) negative lookahead fail match if ._ or -. there in url.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -