Reputation: 859
Let's say I need to have minimum 5 letters in a string not requiring that they are subsequent. The regex below checks subsequent letters
[A-Za-z]{5,}
So, "aaaaa" -- true, but "aaa1aa" -- false.
What is the regex to leave the sequence condition, that both of the strings above would pass as true.
Upvotes: 0
Views: 455
Reputation: 371138
If you have to use a regular expression only, here's one somewhat ugly option:
const check = str => /^(.*[A-Za-z].*){5}/.test(str);
console.log(check("aaaaa"));
console.log(check("aa1aaa"));
console.log(check("aa1aa"));
Upvotes: 1
Reputation: 4005
Allow non-letter characters between the letters:
(?:[A-Za-z][^A-Za-z]*){5,}
Upvotes: 1
Reputation: 627410
You could remove all non-letter chars with .replace(/[^A-Za-z]+/g, '')
and then run the regex:
var strs = ["aaaaa", "aaa1aa"];
var val_rx = /[a-zA-Z]{5,}/;
for (var s of strs) {
console.log( val_rx.test(s.replace(/[^A-Za-z]+/g, '')) );
}
Else, you may also use a one step solution like
var strs = ["aaaaa", "aaa1aa"];
var val_rx = /(?:[^a-zA-Z]*[a-zA-Z]){5,}/;
for (var s of strs) {
console.log( s, "=>", val_rx.test(s) );
}
See this second regex demo online. (?:[^a-zA-Z]*[a-zA-Z]){5,}
matches 5 or more consecutive occurrences of 0 or more non-letter chars ([^a-zA-Z]*
) followed with a letter char.
Upvotes: 4
Reputation: 51481
[a-zA-Z0-9]{5,}
Just like this? Or do you mean it needs to be a regex that ignores digits? Because the above would match aaaa1
as well.
Upvotes: 0