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

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?

 

it should be valid for below scenarios:
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 by keeping everything as is.

Community Alums
Not applicable

Hi @Nandini DM ,

 

Here is the updated regex-

var ipv4Pattern = '((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})(\\/([0-9]|[12][0-9]|3[0-2]))?';
var urlPattern = '((www\\.)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,})';
var combinedPattern = new RegExp('^(' + ipv4Pattern + '|' + urlPattern + ')(\\s*,\\s*(' + ipv4Pattern + '|' + urlPattern + '))*$', 'gm');

var testStrings = [
  '1.1.1.1',
  '11.11.11.11/32',
  '11.22.33.44,11.22.33.44/12',
  'www.example.com',
  'sample.example.com',
  'example.com',
  '11.22.33.44,www.example.com,11.22.33.44/12'
];

function validateInput(input) {
  combinedPattern.lastIndex = 0;
  return combinedPattern.test(input);
}

function runValidation() {
  gs.print('Starting validation...');
  for (var i = 0; i < testStrings.length; i++) {
    var test = testStrings[i];
    var result = validateInput(test);
    gs.print('Validation result for "' + test + '": ' + result);
  }
  gs.print('Validation completed.');
}

runValidation();

Output-

*** Script: Starting validation...
*** Script: Validation result for "1.1.1.1": true
*** Script: Validation result for "11.11.11.11/32": true
*** Script: Validation result for "11.22.33.44,11.22.33.44/12": true
*** Script: Validation result for "www.example.com": true
*** Script: Validation result for "sample.example.com": true
*** Script: Validation result for "example.com": true
*** Script: Validation result for "11.22.33.44,www.example.com,11.22.33.44/12": true
*** Script: Validation completed.

 

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 
Everything is working except for below condition:
Validation result for "*.example.com" is not working, URL should also accept "*" in the initial point.

Community Alums
Not applicable

Hi @Nandini DM ,

Here you go-

// Background script for validating IPv4 addresses, CIDR notations, and URLs

var ipv4Pattern = '((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})(\\/([0-9]|[12][0-9]|3[0-2]))?';
var urlPattern = '((\\*\\.)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,})';
var combinedPattern = new RegExp('^(' + ipv4Pattern + '|' + urlPattern + ')(\\s*,\\s*(' + ipv4Pattern + '|' + urlPattern + '))*$', 'gm');
 
var testStrings = [
  '1.1.1.1',
  '11.11.11.11/32',
  '11.22.33.44,11.22.33.44/12',
  'www.example.com',
  'sample.example.com',
  'example.com',
  '*.example.com',
  'sanjay.servicenow.example.com',
  '11.22.33.44,www.example.com,11.22.33.44/12'
];

function validateInput(input) {
  combinedPattern.lastIndex = 0;
  return combinedPattern.test(input);
}

function runValidation() {
  gs.print('Starting validation...');
  for (var i = 0; i < testStrings.length; i++) {
    var test = testStrings[i];
    var result = validateInput(test);
    gs.print('Validation result for "' + test + '": ' + result);
  }
  gs.print('Validation completed.');
}

runValidation();

 

Output-

*** Script: Starting validation...
*** Script: Validation result for "1.1.1.1": true
*** Script: Validation result for "11.11.11.11/32": true
*** Script: Validation result for "11.22.33.44,11.22.33.44/12": true
*** Script: Validation result for "www.example.com": true
*** Script: Validation result for "sample.example.com": true
*** Script: Validation result for "example.com": true
*** Script: Validation result for "*.example.com": true
*** Script: Validation result for "sanjay.servicenow.example.com": true
*** Script: Validation result for "11.22.33.44,www.example.com,11.22.33.44/12": true
*** Script: Validation completed.

 

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