我有字符串 s,需要检查该字符串是否包含从 a 到 z 的每个字母。检查此条件的正则表达式是什么?其中 s 可以是非常大的字符串,其中包含多个以空格分隔的单词。
例如:
pattern.test("zy aemnofgbc jkhil pqasdfrs tuvrhfwx")
应该返回 true,因为它至少包含所有 a-z 字母表。
pattern.test("sdfasbcd effsdgh ijsfghtkl mnhtop qrsjht uvwmnmx yfdhjkd")
应该返回 false,因为它没有字母 z。
寻找最佳解决方案。
最佳答案
最简单的方法是提取唯一的字符
function testAlphabet(str) {
var chars = str.replace(/[^a-z]/g).split(''); // it may need to convert to lowercase
// remove duplicate
chars = [...new Set(chars)];
console.log(chars.join('').length == 26)
}
testAlphabet("zy aemnofgbc jkhil pqasdfrs tuvrhfwx")
testAlphabet("sdfasbcd effsdgh ijsfghtkl mnhtop qrsjht uvwmnmx yfdhjkd")
关于javascript - 想要在 javascript 中编写正则表达式,它将检查所有提到的字符是否至少存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71701163/