Javascript Pangram Regex

Vontei picture Vontei · Sep 14, 2015 · Viewed 14.4k times · Source

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.

Answer

Tushar picture Tushar · Sep 14, 2015
  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>