REGEX - For Alphanumeric/numeric with potential slash in between - javascript

I am trying to extract a alphanumeric value from a string array using RegEx. Examples:
Apartment 101/B First
Villa 3324/A Second
Milk 12MG/ML Third
Sodium 0.00205MG/ML Fourth
Water 0.00205MG Fifth
Eggs 100 Sixth
My aim is to extract values: 101/B, 3324/A, 12MG/ML, 0.00205MG/ML from the above string arrays. The RegEx I was able to get is something like this: ^[a-zA-Z0-9\.]*$ which is generic alphanumeric regex.

You can match digits followed by optional chars A-Z and then / and uppercase chars
\b\d+(?:\.\d+)?[A-Z]*\/[A-Z]+\b
\b A word boundary to prevent a partial match
\d+(?:\.\d+)? Match 1+ digits with an optional decimal part
[A-Z]* Match optional chars A-Z
\/[A-Z]+ Match / and 1+ chars A-Z
-\b A word boundary
Regex demo
const regex = /\b\d+(?:\.\d+)?[A-Z]*\/[A-Z]+\b/;
[
"Apartment 101/B First",
"Villa 3324/A Second",
"Milk 12MG/ML Third",
"Sodium 0.00205MG/ML Fourth",
].forEach(s => {
const m = s.match(regex);
if (m) {
console.log(m[0]);
}
});
Edit
For matching an optional forward slash and also only digit, you can make the part of the pattern optional.
\b\d+(?:\.\d+)?[A-Z]*(?:\/[A-Z]+)?\b
\b A word boundary
\d+(?:\.\d+)? Match 1+ digits with an optional part
[A-Z]* Match optional uppercase chars A-Z
(?:\/[A-Z]+)? Optionally match / and 1+ uppercase chars A-Z
\b A word boundary
Regex demo

Related

Regex Javascript Starts with AB 0-9 and then ends with A-Z as optional it could be AB00001 or AB00001A (as optional)

^AB+[0-9A-Z?]+$
Javascript Regex Starts with AB 0-9 and then ends with A-Z as optional it could be AB00001 or AB00001A (as optional)
Strings which should work AB00001 , AB000001B , AB12122 , AB00001C with any capital letter as a n optional
The pattern that you tried ^AB+[0-9A-Z?]+$ matches:
Single A char
1+ times B char
1+ times any of the specified ranges 0-9 A-Z or a ? char.
It can match for example ABB???
You could write the pattern matching a single A and single B char, then 1+ digits and optionally match a char A-Z according to the example data.
^AB\d+[A-Z]?$
See a regex demo

Regex for an alphanumeric string of specific length with at least one letter and at least one digit

I want to match alphanumeric string of specific length with at least one letter and at least one digit.
For example, adfyg432 should contain alphabetic and digit and the length should start from 8.
I used this expression but it won't work:
^([A-Za-z]{1,}\d{1,}){8,}$
Your current pattern repeats a group 8 or more times. That group by itself matches 1 or more chars a-z followed by 1 or more digits.
That means that the minimum string length to match is 16 chars in pairs of at least 2. So for example a string like a1aa1a1a1a1a1a1a1 would match.
You could write the pattern using 2 lookahead assertions to assert a length of at least 8 and assert at least a char a-z.
Then match at least a digit. Using a case insensitive match:
^(?=[a-z\d]{8,}$)(?=\d*[a-z])[a-z]*\d[a-z\d]*$
In parts, the pattern matches:
^ Start of string
(?=[a-z\d]{8,}$) Positive lookahead, assert 8 or more chars a-z or digits till end of string
(?=\d*[a-z]) Positive lookahead to assert at least a char a-z
[a-z]* Match optional chars a-z
\d Match at least a single digit
[a-z\d]* Match optional chars a-z or digits
$ End of string
Regex demo
const regex = /^(?=[a-z\d]{8,}$)(?=\d*[a-z])[a-z]*\d[a-z\d]*$/i;
[
"AdfhGg432",
"Abc1aaa"
].forEach(s =>
console.log(`Match "${s}": ${regex.test(s)}`)
)

regular expressions

Is it possible to merge these two regular expressions?
/^[A-Za-z]\S{3,30}$/
/^(?:(\w)(?!\1\1))+$/
I would like a string of:
only letters,
length between 3 and 30,
no spaces,
no to the repetition of the same letter more than two consecutive times (e.g: 'ddd' //false, 'dtdddyyt' //false, 'dtddyyt'//true).
You can use the second pattern as a negative lookahead assertion once to not match ddd in the string.
^(?!\S*(\w)\1\1)[A-Za-z]\S{3,30}$
^ Start of string
(?! Negative lookahead
\S*(\w)\1\1 Match optional non whitespace chars, capture a word char and match the same with 2 backreferences
) Close lookahead
[A-Za-z]\S{3,30} Match a single char A-Za-z and 3-30 non whitespace chars
$ End of string
Regex demo
const regex = /^(?!\S*(\w)\1\1)[A-Za-z]\S{3,30}$/;
[
"ddad",
"dtddyyt",
"adaddd",
"dtdddyyt",
"ddd"
].forEach(s => console.log(`${s} --> ${regex.test(s)}`));
You can use
/^(?:([A-Za-z])(?!\1{2})){3,30}$/
/^(?:(\p{Alphabetic})(?!\1{2})){3,30}$/u
See the regex demo. Note:
only letters - [A-Za-z] / \p{L} or \p{Alphabetic} (available in ECMAScript 2018+ compliant JS environments with /u flag)
length between 3 and 30 - {3,30}
no spaces - this condition is already covered by the first one
no to the repetition of the same letter more than two consecutive times - ([A-Za-z])(?!\1{2}).
JavaScript test:
const rx = /^(?:([A-Za-z])(?!\1{2})){3,30}$/;
const texts = ["ddad","dtddyyt","adaddd","dtdddyyt","ddd"];
for (let text of texts) {
console.log(text, "=>", rx.test(text));
}
More details:
^ - start of string
(?: - start of a non-capturing group (used as a container of a pattern sequence):
([A-Za-z]) - Group 1: an ASCII letter (\p{L} / \p{Alphabetic} matches any Unicode letter)
(?!\1{2}) - right after the letter, there should not be two occurrences of the same letter
) - end of the group
{3,30} - match three to thirty consecutive occurrences of the pattern sequence inside the non-capturing group
$ - end of string.

Updating my regex to include dots and hyphens

My regex code [A-Z]{1,}\d{3,}\w? works fine returning strings like CX3623, M3326, Y2362 but I also want to be able to return strings which are in the following format:
YH321-2
V2021/V2022
1.2A-2351
YGH256-4268
What should I add to the regex?
Demo: https://regex101.com/r/MjPkFh/2
For the first part, you could match the different formats using an alternation.
You could make the second part optional using an optional non capturing group (?:...)? and match either / or - optionally followed by chars A-Z and 1+ digits.
\b(?:[A-Z]+ )?(?:[A-Z]*\d{3,}|\d+(?:\.\d+)?[A-Z]+)(?:[\/-][A-Z]*\d+)?\b
Explanation
\b Word boundary
(?:[A-Z]+ )? Optionally match 1+ chars A-Z followed by a space
(?: Non capture group
[A-Z]*\d{3,} Match 0+ times A-Z and 3 or more digits
| Or
\d+(?:\.\d+)?[A-Z]+ Match 1+ digits with an optional decimal part and 1+ times A-Z
) Close group
(?: Non capture group
[\/-][A-Z]*\d+ Match either / or -, 0+ times A-Z and 1+ digits
)? Close group and make optional
\b Word boundary
Regex demo

Regex for numbers, commas and whitespaces

I need a RegEx that allow the strings that start with numbers separated by comma, finishes with a number (or withspaces after the number) and allow also whitespaces between the number and the comma.
E.g. var str= '1 , 8,9, 88' has to be accetpted while var str2="1 2, 5" has not to be accetped. I tried with var regEx= "^[0-9\,\s]+$"but doing like this it accepts the strings that end with a comma and the strings that have two numbers not separated by comma. Any ideas?
EDIT:
Example of string accepted:
str1= "1,2,3,4"
str2= "1 , 2,3,9"
str3= " 8 , 44, 3 , 11"
Example of string to be discarded:
str4="1, 2,"
str5=", 1,2,"
str6="1,2 3,4"
You could account for the spaces before and after the comma using \s (or just match a space only because \s also matches a newline) to match a whitespace character and use a repeating pattern to match a comma and 1+ digits:
^\s*\d+(?:\s*,\s*\d+)*\s*$
^ Start of string
\s*\d+ Match 0+ whitespace chars and 1+ digits
(?: Non capturing group
\s*,\s*\d+ Match 0+ whitespace chars, and comma, 0+ whitespace chars and 1+ digits
)* Close non capturing group and repeat 0+ times
\s*$ Match 1+ whitespace chars and assert end of string.
Regex demo

Categories

Resources