I am trying to write a REGEX to test for a PANGRAM. I can do it the traditional way, but cannot seem to solve it for more than 90% of my tests with a regular expression.
Input: string
Output: true || false
function isPangram(string){
return ___________________.test(string)
}
Test Results so far.
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
only failed against abcdefghijklmopqrstuvwxyz
6/10 /[\w.]+/
Any help or advice is greatly appreciated.
Pangram strings: A pangram is a string that contains every letter of the English alphabet. We are required to write a JavaScript function that takes in a string as the first and the only argument and determines whether that string is a pangram or not.
A pangram is a sentence containing every letter in the English Alphabet. We have already discussed the naive approach of pangram checking in this article. Now, let's discuss the Pythonic approaches to do the same. This method uses a loop to check if each character of the string belongs to the alphabet set or not.
A pangram or holoalphabetic sentence is a sentence using every letter of a given alphabet at least once. Pangrams have been used to display typefaces, test equipment, and develop skills in handwriting, calligraphy, and keyboarding.
Code:
function isPangram(string) {
var regex = /([a-z])(?!.*\1)/g;
return (string.match(regex) || []).length === 26;
}
Regex101
var regex = /([a-z])(?!.*\1)/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>
This would be a correct answer for the challenge:
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)
}
It uses lookaheads with every letter to check that they are somewhere in the passed string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With