Javascript Pangram 正则表达式
Javascript Pangram Regex
我正在尝试编写一个 REGEX 来测试 PANGRAM。我可以用传统的方式来做,但似乎无法用正则表达式解决超过 90% 的测试。
输入:字符串
输出:真||假
function isPangram(string){
return ___________________.test(string)
}
到目前为止的测试结果。
6/10
/([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, \s]+)/i
6/10
/[a-z]{1}/i
6/10 /[a-z]/i
6/10 /[a-z]+/i
9/10 /a?b?c?d?e?f?g?h?i?j?k?l?m?n?o?p?q?r?s?t?u?v?w?x?y?z/i
仅针对 abcdefghijklmopqrstuvwxyz
失败
6/10 /[\w.]+/
非常感谢任何帮助或建议。
- 将字符串转换为小写
- 使用正则表达式从字符串中提取所有唯一的字母表
- 检查唯一字母的数量是否为 26
代码:
function isPangram(string) {
var regex = /([a-z])(?!.*)/g;
return (string.match(regex) || []).length === 26;
}
var regex = /([a-z])(?!.*)/g;
function check() {
var val = document.getElementById('text').value.toLowerCase();
alert(val.match(regex).length == 26);
}
<input type="text" id="text" />
<button onclick="check()">Check</button>
这将是挑战的正确答案:
function isPangram(string){
return /(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?=.*w)(?=.*x)(?=.*y)(?=.*z)./i.test(string)
}
它对每个字母使用前瞻来检查它们是否在传递的字符串中。
如果您正在寻找非正则表达式解决方案
const isPangram = (string) =>
new Set(string.toLowerCase().match(/[a-z]/g)).size === 26;
console.log(isPangram("abcdefghijklmnopqrstuvwxyz")); //true
console.log(isPangram("The Quick Brown Fox Jumps over the lazy dog")); //true
function isPangram(string){
let lc = string.toLowerCase()
let alphabet = "abcdefghijklmnopqrstuvwxyz"
return alphabet.split("").filter(c => lc.indexOf(c) === -1).length === 0;
}
- 小写字符串
- 用字母创建一个字符串
- 拆分字母表
- 将 filter() 与迷你函数一起使用,该函数将验证其中是否存在 0 个错误语句(对其进行过滤,以便过滤器方法创建的新数组中有 0(零)个错误语句 (-1)“ .lengeth === 0" 确保没有虚假陈述
)
作为单个正则表达式:
/(?:(?=(.*?([a-z]))(?!.*))){26}/i
在 regex101 进行测试。
细分:
/
(?: // non-capturing group
(?= // look-ahead assertion
(.*?([a-z])) // match a letter, preceded by as few characters as possible
(?!.*) // assert that this letter does not appear in the remainder of the string
// (in aggregate, this will find the last occurrence of each letter)
) // actually match that group (this is done to prevent backtracking)
){26} // match 26 times (once for each letter)
/i // match case-insensitively
如果您使用的正则表达式引擎支持 atomic grouping(例如 PCRE),则可以更简洁地编写:
/(?>.*?([a-z])(?!.*)){26}/i
给定一个字符串,检测它是否是 pangram。 Return如果是则为真,否则为假。忽略数字和标点符号。 [根据@Tushar提供的正则表达式添加不区分大小写]
//Detect Pangram
function isPangram(string){
// character set capturing group with negative lookahead
let regex = /([a-z])(?!.*)/gi;
return (string.match(regex)).length === 26;
}
console.log(isPangram("The quick brown fox jumps over the lazy dog."));// true
console.log(isPangram("This is not a pangram."));// false
console.log(isPangram("Pack my box with five dozen liquor jugs."));// true
console.log(isPangram("This isn't a pangram!"));// false
console.log(isPangram("Detect Pangram"));// false
console.log(isPangram("How quickly daft jumping zebras vex."));// true
function isPangram(input) {
if(input.length < 26) {
return false;
}
const letters = 'abcdefghijklmnopqrstuvwxyz';
return Array.from(new Set(input.toLowerCase().split('').filter(i => i.trim()))).sort().join('') === letters;
}
console.log(isPangram("The String is abcdefghijklumnopqrstvwxyz")); //true
使用 for..of 循环 & 包括:
function isPangram(sentence) {
let lowerCased = sentence.toLowerCase();
for (let char of 'abcdefghijklmnopqrstuvwxyz') {
if (!lowerCased.includes(char)) {
return false;
}
}
return true;
}
const isPangram = (string) =>
new Set(string.toLowerCase().match(/[a-z]/g)).size === 26;
console.log(isPangram("This is not a pangram.")); //false
console.log(isPangram("The Quick Brown Fox Jumps over the lazy dog")); //true
我正在尝试编写一个 REGEX 来测试 PANGRAM。我可以用传统的方式来做,但似乎无法用正则表达式解决超过 90% 的测试。
输入:字符串
输出:真||假
function isPangram(string){
return ___________________.test(string)
}
到目前为止的测试结果。
6/10
/([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, \s]+)/i
6/10
/[a-z]{1}/i
6/10 /[a-z]/i
6/10 /[a-z]+/i
9/10 /a?b?c?d?e?f?g?h?i?j?k?l?m?n?o?p?q?r?s?t?u?v?w?x?y?z/i
仅针对 abcdefghijklmopqrstuvwxyz
6/10 /[\w.]+/
非常感谢任何帮助或建议。
- 将字符串转换为小写
- 使用正则表达式从字符串中提取所有唯一的字母表
- 检查唯一字母的数量是否为 26
代码:
function isPangram(string) {
var regex = /([a-z])(?!.*)/g;
return (string.match(regex) || []).length === 26;
}
var regex = /([a-z])(?!.*)/g;
function check() {
var val = document.getElementById('text').value.toLowerCase();
alert(val.match(regex).length == 26);
}
<input type="text" id="text" />
<button onclick="check()">Check</button>
这将是挑战的正确答案:
function isPangram(string){
return /(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)(?=.*w)(?=.*x)(?=.*y)(?=.*z)./i.test(string)
}
它对每个字母使用前瞻来检查它们是否在传递的字符串中。
如果您正在寻找非正则表达式解决方案
const isPangram = (string) =>
new Set(string.toLowerCase().match(/[a-z]/g)).size === 26;
console.log(isPangram("abcdefghijklmnopqrstuvwxyz")); //true
console.log(isPangram("The Quick Brown Fox Jumps over the lazy dog")); //true
function isPangram(string){
let lc = string.toLowerCase()
let alphabet = "abcdefghijklmnopqrstuvwxyz"
return alphabet.split("").filter(c => lc.indexOf(c) === -1).length === 0;
}
- 小写字符串
- 用字母创建一个字符串
- 拆分字母表
- 将 filter() 与迷你函数一起使用,该函数将验证其中是否存在 0 个错误语句(对其进行过滤,以便过滤器方法创建的新数组中有 0(零)个错误语句 (-1)“ .lengeth === 0" 确保没有虚假陈述 )
作为单个正则表达式:
/(?:(?=(.*?([a-z]))(?!.*))){26}/i
在 regex101 进行测试。
细分:
/
(?: // non-capturing group
(?= // look-ahead assertion
(.*?([a-z])) // match a letter, preceded by as few characters as possible
(?!.*) // assert that this letter does not appear in the remainder of the string
// (in aggregate, this will find the last occurrence of each letter)
) // actually match that group (this is done to prevent backtracking)
){26} // match 26 times (once for each letter)
/i // match case-insensitively
如果您使用的正则表达式引擎支持 atomic grouping(例如 PCRE),则可以更简洁地编写:
/(?>.*?([a-z])(?!.*)){26}/i
给定一个字符串,检测它是否是 pangram。 Return如果是则为真,否则为假。忽略数字和标点符号。 [根据@Tushar提供的正则表达式添加不区分大小写]
//Detect Pangram
function isPangram(string){
// character set capturing group with negative lookahead
let regex = /([a-z])(?!.*)/gi;
return (string.match(regex)).length === 26;
}
console.log(isPangram("The quick brown fox jumps over the lazy dog."));// true
console.log(isPangram("This is not a pangram."));// false
console.log(isPangram("Pack my box with five dozen liquor jugs."));// true
console.log(isPangram("This isn't a pangram!"));// false
console.log(isPangram("Detect Pangram"));// false
console.log(isPangram("How quickly daft jumping zebras vex."));// true
function isPangram(input) {
if(input.length < 26) {
return false;
}
const letters = 'abcdefghijklmnopqrstuvwxyz';
return Array.from(new Set(input.toLowerCase().split('').filter(i => i.trim()))).sort().join('') === letters;
}
console.log(isPangram("The String is abcdefghijklumnopqrstvwxyz")); //true
使用 for..of 循环 & 包括:
function isPangram(sentence) {
let lowerCased = sentence.toLowerCase();
for (let char of 'abcdefghijklmnopqrstuvwxyz') {
if (!lowerCased.includes(char)) {
return false;
}
}
return true;
}
const isPangram = (string) =>
new Set(string.toLowerCase().match(/[a-z]/g)).size === 26;
console.log(isPangram("This is not a pangram.")); //false
console.log(isPangram("The Quick Brown Fox Jumps over the lazy dog")); //true