Regex pattern validation for variable number of number pairs - javascript

I am trying to verify that a string is in the correct format...
Number pairs, seperated by a hyphen, each number with a max of 3 digits
No negative numbers
Pairs are seperated by a |
Pattern:
XXX-XXX
XXX-XXX|XXX-XXX|...
Example valid input:
var string1 = "18-200"; // Single entry
var string2 = "18-200|36-90"; // Multiple entries
Example invalid input:
var string3 = "18-2000"; // Failed because the second number has 4 digits
var string4 = "1-1-1-1"; // Failed because there are 4 alternatives, not 2
var string5 = "-20-100"; // Failed because it starts with a negative
I've come up with the following pattern:
^[0-9]+(-[0-9]+)+$
But the expression doesn't match all of the criteria, for example 1-1-1-1 is still okay.

Assuming that:
You always match from the start of the supplied string to the end
You always match in pairs like XXX-XXX
Multiple pairs are separated by |
Then we could write a simplified regex...
^\d-\d(\|\d-\d)*$
Which we can then expand to arrive at...
^\d{1,3}-\d{1,3}(?:\|\d{1,3}-\d{1,3})*$
^ : Start of string
\d{1,3} : Match a number 1 to 3 times
- : Match literally
\d{1,3} : Match a number 1 to 3 times
(?: : Start of non-capturing group
\| : Match literally
\d{1,3} : Match a number 1 to 3 times
- : Match literally
\d{1,3} : Match a number 1 to 3 times
) : End of group
* : Quantifier for 0 or more occurrences of the non-capturing group
$ : End of string
Which we can then test in JS using match to verify...
var regex = /^\d{1,3}-\d{1,3}(?:\|\d{1,3}-\d{1,3})*$/;
var testStrings = [
`1-12`,
`1-12|88-100`,
`1-12|88-100|1-111`,
`1-1-1-1`,
`1-12|`,
`1-12|1-1-2`,
];
for (let num in testStrings) {
console.log(testStrings[num].match(regex));
}
Output, as expected:
["1-12", index: 0, input: "1-12", groups: undefined]
["1-12|88-100", index: 0, input: "1-12|88-100", groups: undefined]
["1-12|88-100|1-111", index: 0, input: "1-12|88-100|1-111", groups: undefined]
null
null
null

One possible approach (for each number up to 3 digits):
^[0-9]{1,3}-[0-9]{1,3}(?:\|[0-9]{1,3}-[0-9]{1,3})*$
Regex101 demo. Essentially, you just repeat the first part of the pattern, preceding it with escaped |, then (with *) make sure the whole group is repeated - or not.
Escaping is important, as | is a metacharacter. And when you use this string in Angular Validator (as it somehow turned out), you should escape the escaping character (\) in your string literal, so it becomes...
requiredPattern: "^[0-9]{1,3}-[0-9]{1,3}(?:\\|[0-9]{1,3}-[0-9]{1,3})*$"

Related

Javascript regex to find two characters between two delimitators

EDITED
I need to find two characters between '[' ']' and '/' '/' using Javascript.
I am using this regex:
([^.][/[string]]|\/string\/)|(\[(string))|(\/(string))| ((string)\])|((string)\/)
that gets two charactes but gets too one character.
The question is, how can I do to get just two characters?
Also I want to get exactly the two characters inside the string, I mean not just only the exact match.
Eg.
User input: dz
It must to find just exact matches that contains "dz", e.g. --> "dzone" but not "dazone". Currently I am getting matches with both strings, "dzone" and "dazone".
Demo: https://regex101.com/r/FEs6ib/1
You could optionally repeat any char except the delimiters between the delimiters them selves, and capture in a group what you want to keep.
If you want multiple matches for /dzone/dzone/ you could assert the last delimiter to the right instead of matching it.
The matches are in group 1 or group 2 where you can check for if they exist.
\/[^\/]*(dz)[^\/]*(?=\/)|\[[^\][]*(dz)[^\][]*(?=])
The pattern matches:
\/ Match /
[^\/]*(dz)[^\/]* Capture dz in group 1 between optional chars other than /
(?=\/) Positive lookahead, assert / to the right
| Or
\[ Match [
[^\][]*(dz)[^\][]* Capture dz in group 2 between optional chars other than [ and ]
-(?=]) Positive lookahead, assert ] to the right
Regex demo
This will match 1 occurrence of dz in the word. If you want to match the whole word, the capture group can be broadened to before and after the negated character class like:
\/([^\/]*dz[^\/]*)(?=\/)|\[([^\][]*dz[^\][]*)(?=])
Regex demo
const regex = /\/[^\/]*(dz)[^\/]*(?=\/)|\[[^\][]*(dz)[^\][]*(?=])/g;
[
"[dzone]",
"/dzone/",
"/dzone/dzone/",
"/testdztest/",
"[dazone]",
"/dazone/",
"dzone",
"dazone"
].forEach(s =>
console.log(
`${s} --> ${Array.from(s.matchAll(regex), m => m[2] ? m[2] : m[1])}`
)
);
If supported, you might also match all occurrences of dz between the delimiters using lookarounds with an infinite quantifier:
(?<=\/[^\/]*)dz(?=[^\/]*\/)|(?<=\[[^\][]*)dz(?=[^\][]*])
Regex demo
const regex = /(?<=\/[^\/]*)dz(?=[^\/]*\/)|(?<=\[[^\][]*)dz(?=[^\][]*])/g;
[
"[adzadzone]",
"[dzone]",
"/dzone/",
"/dzone/dzone/",
"/testdztest/",
"[dazone]",
"/dazone/",
"dzone",
"dazone"
].forEach(s => {
const m = s.match(regex);
if (m) {
console.log(`${s} --> ${s.match(regex)}`);
}
});

Filter version number from string in javascript?

I found some threads about extracting version number from a string on here but none that does exactly what I want.
How can I filter out the following version numbers from a string with javascript/regex?
Title_v1_1.00.mov filters 1
v.1.0.1-Title.mp3 filters 1.0.1
Title V.3.4A. filters 3.4A
V3.0.4b mix v2 filters 3.0.4b
So look for the first occurrence of: "v" or "v." followed by a digit, followed by digits, letters or dots until either the end of the string or until a whitepace occurs or until a dot (.) occurs with no digit after it.
As per the comments, to match the first version number in the string you could use a capturing group:
^.*?v\.?(\d+(?:\.\d+[a-z]?)*)
Regex demo
That will match:
^ Assert the start of the string
.*? Match 0+ any character non greedy
v\.? Match v followed by an optional dot
( Capturing group
\d+ Match 1+ digits
(?: Non capturing group
\.\d+[a-z]? Match a dot, 1+ digits followed by an optional character a-z
)* Close non capturing group and repeat 0+ times
) Close capturing group
If the character like A in V.3.4A can only be in the last part, you could use:
^.*?v\.?(\d+(?:\.\d+)*[a-z]?)
const strings = [
"Title_v1_1.00.mov filters 1",
"v.1.0.1-Title.mp3 filters 1.0.1",
"Title V.3.4A. filters 3.4A",
"V3.0.4b mix v2 filters 3.0.4b"
];
let pattern = /^.*?v\.?(\d+(?:\.\d+[a-z]?)*)/i;
strings.forEach((s) => {
console.log(s.match(pattern)[1]);
});
Details:
v - character "v"
(?:\.)? - matches 1 or 0 repetition of "."
Version capturing group
[0-9a-z\.]* - Matches alphanumeric and "." character
[0-9a-z] - ensures that version number don't ends with "."
You can use RegExp.exec() method to extract matches from string one by one.
const regex = /v(?:\.?)([0-9a-z\.]*[0-9a-z]).*/gi;
let str = [
"Title_v1_1.00.mov filters 1",
"v.1.0.1-Title.mp3 filters 1.0.1",
"Title V.3.4A. filters 3.4A",
"V3.0.4b mix v2 filters 3.0.4b"
];
let versions = [];
let v; // variable to store match
for(let i = 0; i < str.length; i++) {
// Executes a check on str[i] to get the result of first capturing group i.e., our version number
if( (v = regex.exec(str[i])) !== null)
versions.push(v[1]); // appends the version number to the array
// If not found, then it checks again if there is a match present or not
else if(str[i].match(regex) !== null)
i--; // if match found then it loops over the same string again
}
console.log(versions);
var test = [
"Title_v1_1.00.mov filters 1",
"v.1.0.1-Title.mp3 filters 1.0.1",
"Title V.3.4A. filters 3.4A",
"V3.0.4b mix v2 filters 3.0.4b",
];
console.log(test.map(function (a) {
return a.match(/v\.?([0-9a-z]+(?:\.[0-9a-z]+)*)/i)[1];
}));
Explanation:
/ # regex delimiter
v # letter v
\.? # optional dot
( # start group 1, it will contain the version number
[0-9a-z]+ # 1 or more alphanumeric
(?: # start non capture group
\. # a dot
[0-9a-z]+ # 1 or more alphanumeric
)* # end group, may appear 0 or more times
) # end group 1
/i # regex delimiter and flag case insensitive

Recursively patten js

I want to check a recursively text that verufy three rules.
1º: All the string should be a sequence of numbers between 0-31 + a dot .
Example: 1.23.5.12
2º: The string can't begin or end with a dot.
Like this.
.1.23.5.12.
3º You can write a max of 51 digits (following the previous rules)
I tried to make a pattern to my js function. But this dont work.
This is my function:
var str = document.getElementById("numero").value;
var patt1 = /^[0-9]+\./g;
var result = str.match(patt1);
document.getElementById("demo").innerHTML = result;
What is wrong in the pattern?
You may use
/^(?!(?:\D*\d){52})(?:[12]?\d|3[01])(?:\.(?:[12]?\d|3[01]))*$/
See the regex demo
Details
^ - start of string
(?!(?:\D*\d){52}) - fail if there are 52 or more digits separated with any 0+ non-digits
(?:[12]?\d|3[01]) - 1 or 2 (optional) followed with any single digit or 3 followed with 0 or 1 (0 - 31)
(?:\.(?:[12]?\d|3[01]))* - zero or more consecutive repetitions of
\. - dot
(?:[12]?\d|3[01]) - see above (0 - 31)
$ - end of string.
Use it with test:
if (/^(?!(?:\D*\d){52})(?:[12]?\d|3[01])(?:\.(?:[12]?\d|3[01]))*$/.test(str)) {
// Valid!
}
Test:
var rx = /^(?!(?:\D*\d){52})(?:[12]?\d|3[01])(?:\.(?:[12]?\d|3[01]))*$/;
var strs = [".12", "123", "1.23.5.12", "12345678"];
for (var s of strs) {
console.log(s, "=>", rx.test(s));
}
The regex ^[0-9]+\. matches from the start of the string ^ one or more digits [0-9]+ followed by a dot \.
You might use:
^(?!(\.?\d){52})(?:[0-9]|[12][0-9]|3[01])(?:\.(?:[0-9]|[12][0-9]|3[01]))+$
Explanation
^ Assert the start of the line
(?!(\.?\d){52}) Negative lookahead to assert that what follows is not 52 times an optional dot followed by one or more digits
(?:[0-9]|[12][0-9]|3[01]) Match a number 0 - 31
(?:\.(?:[0-9]|[12][0-9]|3[01]))+ Repeat in a group matching a dot followed by a number 0 - 31 and repleat that one or more times so that a single digit wihtout a dot does not match
$ Assert the end of the string
const strings = [
'1.23.5.12',
'1.23.5.12.',
'.1.23.5.12.',
'1.23.5.12',
'1',
'1.23.5.12.1.23.5.1.23.5.12.1.23.5.1.23.5.12.1.23.5.1.23.5.12.1.23.5.1.23.5.12.1.23.5.2',
'1.23.5.12.1.23.5.12.1.23.5.12.1.23.5.12.1.23.5.12.1.23.5.12.1.23.5.12.1.23.5.12.1.23.5.12'
];
let pattern = /^(?!(\.?\d){52})(?:[0-9]|[12][0-9]|3[01])(?:\.(?:[0-9]|[12][0-9]|3[01]))+$/;
strings.forEach((s) => {
console.log(s + " ==> " + pattern.test(s));
});

validate text field using regular expression javaScript

I want to validate a text field to accept just text like this :
1,2;2,3;1-3
1-2;4;2,3;4;1-3
12
I don't want the types like this :
;1
,1
-1
1;;2
1,,2
1--2
1-2-3
1,2,3
1,2-3
so I make this regular expression but it seems doesn't work like what I want
var reg = /^\d*(((?!.*--)(?!.*,,)(?!.*;;)(?!.*,;)(?!.*,-)(?!.*-;)(?!.*-,)(?!.*;,)(?!.*;-))[,-;])*\d$/
thanks for your help :)
you can simply use the regex
function match(str){
return str.match(/^(?!.*([-,])\d+\1)(?!.*,\d+-)\d+(?:[-,;]\d+)*$/) != null
}
console.log(match(';1'));
console.log(match(',1'));
console.log(match('1;;2'));
console.log(match('1-3'));
console.log(match('12'));
console.log(match('1,2;2,3;1-3'));
console.log(match('1-2;4;2,3;4;1-3'));
console.log(match('1,2,3'));
take a look at regex demo
Here's my attempt. Based on your examples I've assumed that semi-colons are used to separate 'ranges', where a 'range' can be a single number or a pair separated by either a comma or a hyphen.
var re = /^\d+([,\-]\d+)?(;\d+([,\-]\d+)?)*$/;
// Test cases
[
'1',
'1,2',
'1-2',
'1;2',
'1,2;2,3;1-3',
'1-2;4;2,3;4;1-3',
'12',
';1',
',1',
'-1',
'1;;2',
'1,,2',
'1--2',
'1-2-3',
'1,2,3',
'1,2-3'
].forEach(function(str) {
console.log(re.test(str));
});
The first part, \d+([,\-]\d+)? matches a 'range' and the second part (;\d+([,\-]\d+)?)* allows further 'ranges' to be added, each starting with a semi-colon.
You can add in ?: to make the groups non-capturing if you like. That's probably a good idea but I wanted to keep my example as simple as I could so I've left them out.
You may use
/^\d+(?:(?:[-,;]\d+){3,})?$/
See the regex demo
Details
^ - start of string
\d+ - 1 or more digits
(?:(?:[-,;]\d+){3,})? - 1 or 0 sequences of:
(?:[-,;]\d+){3,} - 3 sequences of:
[-,;] - a -, , or ;
\d+ - 1 or more digits
$ - end of string
var ss = [ '1,2;2,3;1-3','1-2;4;2,3;4;1-3','12',';1',',1','-1','1;;2','1,,2','1--2','1-2-3','1,2,3','1,2-3',';1',',1','-1','1;;2','1,,2','1--2' ];
var rx = /^\d+(?:(?:[-,;]\d+){3,})?$/;
for (var s of ss) {
console.log(s, "=>", rx.test(s));
}
NOTE: the [,-;] creates a range between , and ; and matches much more than just ,, - or ; (see demo).

Javascript regex that matches one pattern in string but exclude another

I have two possible strings that I need to match:
+/-90000
and
+9000 / -80000
I need to recognise the two patterns separately so wrote some regex for this. The first single number string I can match like so:
/\+\/\-{1}/g
And i wrote this for the second:
/(\+(?=[0-9]+){1}|\-(?=[0-9]+){1}|\/(?=\s){1})/g
The second would also partially match the first the first number i.e. the -90000. Is there a way that they can be improved so that they match exclusively?
You can use a single expression:
^(?:(\+\/-\s*\d+)|((\+\s*\d+)\s*\/\s*(-\s*\d+)))$
The only restriction you'll have to work with would be that in the second type of input, the positive number should come first.
You'll get the matched group in matches[1] if the input was of type 1, and in matches[2] if it was of type 2. For the type-2 input, further matches of each number gets stored in matches[3] and matches[4].
You can see the demo on regex101.
Here are two solutions with slightly different semantics.
With the first, if the string is type 1 the number will be in capture group 1 (result[1]) and if it's type 2 the numbers will be in capture groups 2 and 3 (and capture group 1 will be null). The test for type 1, then, is result[1] !== null.
var a = '+/-90000';
var b = '+9000 / -80000';
var result;
var expr1 = /\+(?:\/-(\d+)|(\d+) \/ -(\d+))/;
result = a.match(expr1);
// => [ '+/-90000', '90000', null, null ]
result = b.match(expr1);
// => [ '+9000 / -80000', null, '9000', '80000' ]
With the second, if the string is type 1 the number will be in capture group 1 (and capture group 2 will be null), and if it's type 2 the numbers will be in capture groups 2 and 3. The test for type 1 is result[1] === null.
var expr2 = /\+(\d+ )?\/ ?-(\d+)/;
result = a.match(expr2);
// => [ '+/-90000', null, '90000' ]
result = b.match(expr2);
// => [ '+9000 / -80000', '9000', '80000' ]

Categories