Regular Expression With No More Than 3 Alphabets Concurrently Should Not Accept 1a1a1a1a1
should not accept: 1a1a1a1a1, aaaa11111 ,2222aaa33a only allow 3 characters anywhere no more than three should not allowed I tried like below but failed var patt = new RegExp('([A-
Solution 1:
Your regex needs to account for non-letters in between the letters. I would do that with this regex:
^(?!.*([a-z].*){4,}).*$
This way you can easily add more requirements by adding another (?!...)
or (?=...)
lookaround.
Solution 2:
^(?!.*(.)(?:.*\1){3,}).*$
Try this.See demo.The looahead makes sure any character is not there more than 3 times.
https://regex101.com/r/wU7sQ0/10
var re = /^(?!.*(.)(?:.*\1){3,}).*$/gm;
var str = '1a1a1a1a1\naaaa11111\n2222aaa33a\nsadsada';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.// eg m[0] etc.
}
Solution 3:
functiontestNumberOfChar(str){
var match= str.match(/[a-zA-Z]/g);
if( match && match.length > 3 ){
alert("No more than three alphabets are allowed");
returnfalse;
}
returntrue;
}
Solution 4:
You can do this:
if (/^(?:[^a-z]*[a-z]){4}/i.test(DLnumber)) {
alert("No more than three alphabets are allowed");
returnfalse;
}
- the i modifier makes the pattern case-insensitive, so
[a-z]
is the same than[a-zA-Z]
and[^a-z]
the same than[^a-zA-Z]
for all the pattern. ^
is the anchor for the start of the string (not needed but improve performances in particular when the pattern must fail)(?:...)
is an non-capturing group, (it does nothing, its only task is to put other tokens together, then you can apply a quantifier to all the group.)(?:[^a-z]*[a-z]){4}
means a group of zero or more non-letters followed by a letter and repeated 4 times
Solution 5:
I thin this may be what you look for:
if (str.replace(/[^a-z]/gi, "").length>3) {
alert("No more than three alphabets are allowed");
returnfalse;
}
It counts the Alphabetical chars in the string...
Post a Comment for "Regular Expression With No More Than 3 Alphabets Concurrently Should Not Accept 1a1a1a1a1"