Regex Validation for IP address || CIDR || URL

Nandini DM
Tera Contributor

 

Hi,
I want to join below two regex pattern with comma separated. I have IPV4 and CIDR with comma separated but want to add URL to the same expression so that if user try to add any one of the IP/CIDR/URL .it should allow user to add with comma separated.
 
 
1)  IP Address and CIDR with comma separated : 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[0-9]|2[0-8]|3[0-2])){0,1}){0,1}((\\s*,\\s*)(?=[^,])){0,1})+$', 'gm');
 
How to join the below pattern to the above pattern so that we get all IPV4 ,CIDR and URL with comma separated 
 
2) URL :  new RegExp('^(www\\.)?((?:(?!www\\.|\\.).)+\\.[a-zA-Z0-9.]+)', 'gm');
 
Please Help!
 
Thanks!
8 REPLIES 8

Peter Bodelier
Giga Sage

Hi @Nandini DM,

 

Could you provide some examples of what you would like to see matched?
Your first regex isn't working for what I would expect it to validate.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi @Peter Bodelier 
First Regex will match for
1.1.1.1 --> Valid
11.11.11.11/32 --> Valid
11.22.33.44,11.22.33.44/12 --> Valid

Same way I want  below to work
11.22.33.44,11.22.33.44/12,www.sample.com

Community Alums
Not applicable

Hi @Nandini DM ,

 

Can you please try the below regex.

 

 

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');

 

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar

 

Hi @Community Alums 

In the below expression: 

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');

 

How to make below URL to be Valid?
www.sample.com --> Valid

Sample.sample.com --> Valid (without www, it should allow 2 dots)

Sample.com -->Valid
How to change above expression to change URL  only to make it possible