Answer the question
In order to leave comments, you need to log in
How to combine regular expressions?
Hello.
The task is as follows: in the input field we enter either an IP or a URL. They need to be validated, for which I decided to use regular expressions, putting them in a separate file, so that it would be more convenient to manage them.
The file is now written as follows:
const urlPattern = '^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$';
const ipPattern =
'^(1?\d{1,2}|2[0-4]\d|25[0-5])\.' +
'(1?\d{1,2}|2[0-4]\d|25[0-5])\.' +
'(1?\d{1,2}|2[0-4]\d|25[0-5])\.' +
'(1?\d{1,2}|2[0-4]\d|25[0-5])\/?(\b|\/32)$';
export const urlPatternExp: RegExp = new RegExp(urlPattern);
export const ipPatternExp: RegExp = new RegExp (ipPattern);
export const ipOrUrlPatternExp: RegExp = new RegExp (`(${urlPattern})|(${ipPattern})`, 'g');
(^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$)|(^(1?\d{1,2}|2[0-4]\d|25[0-5])\.(1?\d{1,2}|2[0-4]\d|25[0-5])\.(1?\d{1,2}|2[0-4]\d|25[0-5])\.(1?\d{1,2}|2[0-4]\d|25[0-5])\/?(\b|\/32)$)
- name.com
- 192.168.1.1
url: [
'',
[ Validators.required, Validators.pattern(ipOrUrlPatternExp) ]
],
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question