Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Pangram Regex

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.

like image 569
Vontei Avatar asked Sep 14 '15 04:09

Vontei


People also ask

How do you check if a string is a pangram in JS?

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.

Is pangram a function?

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.

What is pangram sentence?

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.


2 Answers

  1. Convert the string to lowercase
  2. Use regex to extract all the unique alphabets from string
  3. Check if the no of unique alphabets are 26

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>
like image 191
Tushar Avatar answered Sep 30 '22 04:09

Tushar


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.

like image 36
zerkms Avatar answered Sep 30 '22 04:09

zerkms