Regex for numbers, commas and whitespaces - javascript

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

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 Alphanumeric/numeric with potential slash in between

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

Regex to not allow special character without prefix or suffix

I was writing regex for the following validate a string. I wrote the following regex.
^[^\s]+[a-z]{0,}(?!.* {2})[ a-zA-z]{0,}$
it validates for
No space in beginning.
no two consecutive space allowed.
The problem is it allows a single special character. it should not allow a special character unless it is suffixed or prefixed with alpha-numeric character.
Examples:
# -> not allowed.
#A or A# or A2 or 3A is allowed.
One option is to assert that the string does not contain a single "special" char or 2 special chars next to each other using a negative lookahead.
^(?!.*[^a-zA-Z0-9\s][^a-zA-Z0-9\s])(?!.*(?:^| )[^a-zA-Z0-9\s](?!\S))\S+(?: \S+)*$
Explanation
^ Start of string
(?! Negative lookahead, assert that what is at the right does not contain
.*[^a-zA-Z0-9\s][^a-zA-Z0-9\s] match 2 chars other than a-zA-Z0-9 or a whitespace char next to each other
) Close lookahead
(?! Negative lookahead, assert that what is at the right does not contain
.*(?:^| )[^a-zA-Z0-9\s](?!\S) Match a single char other than a-zA-Z0-9 or a whitespace char
) Close lookahead
\S+(?: \S+)* Match 1+ non whitespace chars and optionally repeat a space and 1+ non whitespace chars
$ End of string
Regex demo
Please omit the '$' symbol from the regex because it represents the end of the sentence.
^[^\s]+[a-z]{0,}(?!.* {2})[ a-zA-z]{0,}
So when applying the above regex to the following, it finds only '# '.
#A A# A2 3A

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 to disallow certain characters in specific sequence

I have a regular expression that allows only letters, numbers, spaces or hyphens. However, I'd like to disallow the user to do the following:
hello--world Have more than one hyphen sitting next to each other
--hello Have a hyphen in the beginning. It must have a number or letter first
How do I accomplish this? My current regex looks like this:
let alphanumericTest = new RegExp("^\s*([0-9a-zA-Z- ]*)\s*$");
You can try this regex expression. ^\s*[0-9a-zA-Z](?:(?!--)[0-9a-zA-Z- ])*$
This is a demo.
You could make you match a bit more efficient without using a negative lookahead for matching non consecutive hyphens using repeating groups which can optionally start with an hyphen after the first word.
^[ ]*[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)*-?(?:[ ]+-?(?:[0-9a-zA-Z]+-?)*)*$
(Used [ ] to match a space for clarity)
Explanation
^ Start of string
[ ]* Match 0+ spaces
[0-9a-zA-Z]+ Match 1+ times any of the listed
(?:-[0-9a-zA-Z]+)* Repeat 0+ times matching a hyphen and 1+ what is listed
-? Match optional hyphen
(?: Non capturing group
[ ]+-?(?:[0-9a-zA-Z]+-?)* Match 1+ spaces, optional hyphen, repeat 0+ times what is listed and optional hyphen
)* Close outer non capturing group and repeat 0+ times
$ End of string
Regex demo
Try:
let alphanumericTest = new RegExp("^(?!-)(?!.*--)[0-9a-zA-Z- ]+(?<!-)$");
This checks that the first character is not a - and that there are no consecutive --s anywhere in the string

Categories

Resources