Regex Validation

Nandini DM
Tera Contributor

In the below expression: 
var a = new RegExp('^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([1-9]|[1-2][0-9]|3[0-2]))?|((www\\.)?((?:(?!www\\.|\\.).)+\\.[a-zA-Z0-9]+)))(\\s*,\\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([1-9]|[1-2][0-9]|3[0-2]))?|((www\\.)?((?:(?!www\\.|\\.).)+\\.[a-zA-Z0-9]+))))*$', 'gm');

 

Above pattern will match for IP/CIDR and URL with comma separated , I am facing one small issue 
IP and CIDR is working all good but how to make URL valid for below scenarios:
www.sample.com --> Valid
Sample.sample.com --> Valid (without www, it should allow 2 dots)
Sample.com -->Valid

11.11.11.11,22.22.22.22/32,www.sample.com,sample.sam.com --> valid

How to change above expression to make it work for all above scenarios 

 

Kindly Help!

2 REPLIES 2

Dnyaneshwaree
Mega Sage

Hello @Nandini DM ,

Try this one:
var a = new RegExp('^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([1-9]|[1-2][0-9]|3[0-2]))?|((www\\.)?([a-zA-Z0-9]+\\.){1,2}[a-zA-Z0-9]+))(\\s*,\\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([1-9]|[1-2][0-9]|3[0-2]))?|((www\\.)?([a-zA-Z0-9]+\\.){1,2}[a-zA-Z0-9]+)))*$', 'gm'

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

Hi @Dnyaneshwaree 
along with the above if user try to enter "*" like <*.sample.com> it should be valid

 

Sample : *.sample.sample --> valid


How make it possible with the below expression
var a = new RegExp('^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([1-9]|[1-2][0-9]|3[0-2]))?|((www\\.)?([a-zA-Z0-9]+\\.){1,2}[a-zA-Z0-9]+))(\\s*,\\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([1-9]|[1-2][0-9]|3[0-2]))?|((www\\.)?([a-zA-Z0-9]+\\.){1,2}[a-zA-Z0-9]+)))*$', 'gm'