How do you check whether a string contains all alphabets in javascript?

This is the simplest solution I found. It returns true or false for the given string if it contains all letters of the alphabet in it or not.

Here is the code I found:

new Set("A quick brown fox jumps over the lazy dog"
  .toLowerCase()
  .replace(/[^a-z]/gi, "")
  .split("")
).size === 26

Any other simpler form of checking to see if a string contains all of the letters in the alphabet would be helpful.

Thanks!

Andria

4,0122 gold badges21 silver badges37 bronze badges

asked Mar 23, 2019 at 6:45

How do you check whether a string contains all alphabets in javascript?

6

You don't need to split

As it would turn out, you don't need to run String#split before passing your string to new Set. The constructor for Set, when passed a string, will, essentially, split it into single characters for you before creating the set.

Example:

new Set('A quick brown fox jumps over the lazy dog'
  .toLowerCase()
  .replace(/[^a-z]/g, '')
).size === 26

This works just as well because something like new Set('test') turns into

Set(3) {"t", "e", "s"}

On a side note, you can see that I've removed the i flag from the regular expression as pointed out by one of the other answers as it is unnecessary due to the .toLowerCase()

answered Mar 23, 2019 at 7:19

AndriaAndria

4,0122 gold badges21 silver badges37 bronze badges

1

You can avoid the regex and also return early from the function once you have all the letters with something like this. It creates a set of all the letters and removes them as you find them. Once the set is empty you can return. If the loop finishes, you didn't remove everything. This only requires space for the alphabet set and since set operations are constant time, this is O(n) in the worst case.

function allLetters(str){
    let alpha = new Set("abcdefghijklmnopqrstuvwxyz")
    for (let c of str.toLowerCase()){
        alpha.delete(c)
        if (alpha.size == 0) return true
    }
    return false
}

let text = "hello my name if gunther"
let text2 = "The quick brown fox jumps over the lazy dog"

console.log(allLetters(text))
console.log(allLetters(text2))

answered Mar 23, 2019 at 7:08

How do you check whether a string contains all alphabets in javascript?

MarkMark

86.7k6 gold badges96 silver badges140 bronze badges

3

This is the simplest code I found, It returns true or false for the given string mentioning the string contains all the alphabet in it or not.

new Set("".toLowerCase().replace(/[^a-z]/g, "") ).size === 26

Example:

new Set("A quick brown fox jumps over the lazy dog".toLowerCase().replace(/[^a-z]/g, "") ).size === 26

Any other simplest form of code can be helpful. Please share it.

Thanks!

answered Mar 23, 2019 at 6:50

How do you check whether a string contains all alphabets in javascript?

rockey91rockey91

1322 silver badges10 bronze badges

3

I believe this is the "simplest" w.r.t. computational complexity, requiring O(1) space (to store the character frequency table, assuming a fixed upper-bound possible input alphabet) and O(n) time as it iterates over the input string only once (plus a constant-time for the final check over the alphabet string).

var inputString = "Jaded zombies acted quaintly but kept driving their oxen forward";

var charCounts = {};
for( var i = 0; i < inputString.length; i++ ) {
    var c = inputString.at( i ).toLower();
    if( charCounts[c] ) charCounts[c]++;
    else                charCounts[c] = 1;
}

var alphabet = "abcdefghijklmnopqrstuvwyz";
for( var a = 0; a < alphabet.length; a++ ) {
    if( !charCounts[ alphabet.at(a) ] ) {
        console.log( "char %s does not appear in input string.", alphabet.at(a) );
    }
}

answered Mar 23, 2019 at 6:50

How do you check whether a string contains all alphabets in javascript?

DaiDai

131k25 gold badges231 silver badges336 bronze badges

1

As I look at it again, I can provide one tiny improvement to your code:

new Set("".toLowerCase().replace(/[^a-z]/g, "").split("")).size === 26 .

Remove the 'i' flag on the regex because it's lowercased.

answered Mar 23, 2019 at 7:11

How do you check whether a string contains all alphabets in javascript?

Chris HappyChris Happy

6,7731 gold badge20 silver badges44 bronze badges

1

Here is a different way to due it using String.fromCharCode() and every()

const allLetters = (str) => Array.from({length:26}).map((x,i) => String.fromCharCode(i+97)).every(a => str.toLowerCase().includes(a));
console.log(allLetters("abcdefghijklmnopqrstuvwxyz"));

Or you can hardcode all the alphabets.

const allLetters = (str) => [..."abcdefghijklmnopqrstuvwxyz"].every(x => str.toLowerCase().includes(x));

console.log(allLetters('abcdefghijklmnopqrstuvwxyz'))
console.log(allLetters('abcdefghijklmnopqyz'))

answered Mar 23, 2019 at 7:40

How do you check whether a string contains all alphabets in javascript?

Maheer AliMaheer Ali

34.7k5 gold badges38 silver badges67 bronze badges

function isPangram(sentence){
   let lowerCased = sentence.toLowerCase();
   for(let char of 'abcdefghijklmnopqrstuvwxyz'){
      if(!lowerCased.includes(char)){
         return false
       }
    }
  return true
}

Here is another way using a for...of loop.

answered Dec 22, 2019 at 23:25

How do you check if a string contains all alphabets in JS?

Checking for all letters.
Javascript function to check for all letters in a field function allLetter(inputtxt) { var letters = /^[A-Za-z]+$/; if(inputtxt.value.match(letters)) { return true; } else { alert("message"); return false; } } ... .
Flowchart:.
HTML Code

How do you check whether a string contains all alphabets?

To check if String contains only alphabets in Java, call matches() method on the string object and pass the regular expression "[a-zA-Z]+" that matches only if the characters in the given string is alphabets (uppercase or lowercase).

How do you check if a string contains only alphabets and numbers in JavaScript?

The RegExp test() Method To check if a string contains only letters and numbers in JavaScript, call the test() method on this regex: /^[A-Za-z0-9]*$/ . If the string contains only letters and numbers, this method returns true . Otherwise, it returns false .

How do you check if all characters in string are numbers JavaScript?

To check if a string contains numbers in JavaScript, call the test() method on this regex: /\d/ . test() will return true if the string contains numbers. Otherwise, it will return false . The RegExp test() method searches for a match between a regular expression and a string.