How do I combine these three regex into one? - javascript

I'm trying to make a regular expression that only accepts:
Min 100 and atleast 1000 characters, characters “,’,<,> aren't allowed, two full stops one after another aren't allowed.
This is what I have for now:
^.{100,1000}$ → for 100 to 1000 characters
^[^"'<>]*$ → for the characters that aren't allowed
^([^._]|[.](?=[^.]|$)|_(?=[^_]|$))*$ → doesn't allow 2 consecutive dots
How do I combine this regex into one? ._.

This part [^._] means no dot or underscore and this part [.](?=[^.]|$)|_(?=[^_]|$) matches either a . or _ followed by the opposite or end of string.
You could write the pattern using a single negative lookahead assertion excluding __ or ..
^(?!.*([._])\1)[^"'<>\n]{100,1000}$
Explanation
^ Start of string
(?! Negative lookahead, assert that what is at the right is not
.*([._])\1 capture either . or _ and match the same captured char after it (meaning no occurrence of .. or __)
) Close lookahead
[^"'<>\n]{100,1000} Match 100-1000 times any character except the listed
$ End of string
Regex demo (with the quantifier set to {10,100} for the demo)

Related

How to create a regex pattern equal to 1,2,3

Currently, on the project, they have a pattern [^0-9,] and it replaces (String.prototype.replace) everything that we don't need. Except it's not so great. We can add a comma to the start and to the end of the string.
What do I need and I can't do it no matter how hard I try)
first should be a number in the range 0-9
after the first number should be a comma (one comma) or a number or numbers (0-9)
at the end of the line shouldn't be a comma
Correct example,
1,2,3
Incorrect,
,,,,1,,2dgd,d,
1,2,3,
,1,2,,,3
UPD: The method String.prototype.replace() is used on the project.
I'll be grateful if you help me!
If the comma's are optional, maybe you need:
^\d+(?:,\d+)*$
See the online demo
^ - Start string ancor
\d+ - One or more digits.
(?:- Open non-capture group.
,\d+ - A comma followed but one or more digits.
)* - Close non-capture group and match zero or more times.
$ - End string ancor.
Edit:
If you actually want to clean a string, maybe you could use:
(\d+).*?(,(?=.*\d))|\D
See the online demo. Just make sure to replace by $1$2.
(\d+) - 1st Capture group with one or more digits.
.*? - Lazy match anything opto:
(,(?=.*\d)) - 2nd Capture group to match literal comma with a nested positive lookahead to ensure there is still a digit ahead.
| - Or:
\D - Anything other than digit.
const regexp = new RegExp(/(\d+).*?(,(?=.*\d))|\D/g);
const value = '1,2,3'.replace(regexp, '$1$2');
console.log(value)
This should work:
^[0-9]+(?:,[0-9]+)*$
Begins with a digit or a set of digits, followed by zero or more occurrences of a comma followed by one or more digits. Don't miss the start and end line anchors.
Demo

Do not allow '.'(dot) anywhere in a string (regular expression)

I have a regular expression for allowing unicode chars in names(Spanish, Japanese etc), but I don't want to allow '.'(dot) anywhere in the string.
I have tried this regex but it fails when string length is less than 3. I am using xRegExp.
^[^.][\\pL ,.'-‘’][^.]+$
For Example:
NOËL // true
Sanket ketkar // true
.sank // false
san. ket // false
NOËL.some // false
Basically it should return false when name has '.' in it.
Your pattern ^[^.][\\pL ,.'-‘’][^.]+$ matches at least 3 characters because you use 3 characters classes, where the first 2 expect to match at least 1 character and the last one matches 1 or more times.
You could remove the dot from your character class and repeat that character class only to match 1+ times any of the listed to also match when there are less than 3 characters.
^[\p{L} ,'‘’-]+$
Regex demo
Or you could use a negated character class:
^[^.\r\n]+$
^ Start of string
[^.\r\n]+ Negated character class, match any char except a dot or newline
$ End of string
Regex demo
You could try:
^[\p{L},\-\s‘’]+(?!\.)$
As seen here: https://regex101.com/r/ireqbW/5
Explanation -
The first part of the regex [\p{L},\-\s‘’]+ matches any unicode letter, hyphen or space (given by \s)
(?!\.) is a Negative LookAhead in regex, which basically tells the regex that for each match, it should not be followed by a .
^[^.]+$
It will match any non-empty string that does not contain a dot between the start and the end of the string.
If there is a dot somewhere between start to end (i.e. anywhere) it will fail.

Issue with javascript regex not matching less than 3 characters

I have the following javascript regex:
/^[^\s][a-z0-9 ]+[^\s]$/i
I need to allow any alphanumeric character as well as spaces inside the string but not at the beginning nor at the end.
Oddly enough, the above regex will not accept less than 3 characters, e.g. aa will not match but aaa will.
I am not sure why. Can anyone please help ?
You have: [^\s] (requires matching at least one non-whitespace character), [a-z0-9 ]+ (requires matching at least one alphanumeric or space character), and [^\s] again (requires matching at least one non-whitespace character). So, in total, you need at least 3 characters in the string.
Use word boundaries at the beginning and end instead:
/^\b[a-z0-9 ]+\b$/i
https://regex101.com/r/2GhH3N/1
Try the following regex:
^(?! )[a-z0-9 ]*[a-z0-9]$
Details:
^(?! ) - Start of the string and no space after it (so here we exclude the
initial space).
[a-z0-9 ]* - A sequence of letters, digits and spaces, possibly empty
(the content before the last letter(see below).
[a-z0-9]$ - The last letter and the end of string (so here we exclude the
terminal space).
You should re-write the expression as
/^[a-z0-9]+(?:\s+[a-z0-9]+)*$/i
See the regex demo.
NOTE: If only one whitespace is allowed between the alphanumeric chars use
/^[a-z0-9]+(?:\s[a-z0-9]+)*$/i
^^
Details
^ - start of string
[a-z0-9]+ - 1+ letters/digits
(?:\s+[a-z0-9]+)* - 0 or more repetitions of 1+ whitespaces (\s+) and 1+ digit/letters
$ - end of string.
See the regex graph:

Regex that allows a single whitespace in the middle but with character limit.

Excuse my ignorance but I really need help with this, I need this regex: [A-Za-z0-9]+\s?[A-Za-z0-9]+ (an username that allows a single whitespace in the middle, but not at the beginning or at the end.), but limiting the total amount of characters to minimum 3 and maximun 30.
I have tried to adapt this answer using negative lookaheads, but so far is not working.
It has to be a regex, it can't use jQuery or anything else.
You may use a positive lookahead here:
^(?=.{3,30}$)[A-Za-z0-9]+(?:\s[A-Za-z0-9]+)?$
See the regex demo.
Details:
^ - start of string
(?=.{3,30}$) - there can be 3 to 30 chars (other than linebreak, replace the . with [A-Za-z0-9\s] to be more specific)
[A-Za-z0-9]+ - 1+ alphanumeric chars
(?:\s[A-Za-z0-9]+)? - an optional (1 or 0) occurrences of a
\s - whitespace
[A-Za-z0-9]+ - 1+ alphanumeric symbols
$ - end of string.
You can use:
(?=^[A-Za-z0-9]+\s?[A-Za-z0-9]+$).{3,30}
See a demo on regex101.com. It will match:
username123 # this one
user name 123 # this one not (two spaces!)
user name123 # this one
u sername123 # this one
username123 # this one not (space in the beginning!)

How to build a custom regex that matches dashes/alphanumeric characters and '.' dot characters that are not consecutive?

I need to build a regex that doesn't match the words with this requirements:
at least 3 characters
maximum 32 characters
only a-z0-9_-.
dots: . ok, .. nope
this is what i did:
/[0-9a-zA-Z\-\_\.]{3,32}/
the problem is that i can insert more than one . and i don't know how to fix it.
You could use the following expression:
/(?:[\w-]|\.(?!\.)){3,32}/
Explanation:
(?: - Start of a non-capturing group
[\w-] - Character set to match [a-zA-Z0-9_-]
| - Alternation, or..
\.(?!\.) - Negative lookahead to match a . character literally if it isn't followed by another . character.
) - Close the non-capturing group
{3,32} - Match the group 3 to 32 times
You may also want to add anchors if you want to match the entire string against the expression:
/^(?:[\w-]|\.(?!\.)){3,32}$/

Categories