cdarwin
cdarwin

Reputation: 4291

Detecting if a character is a letter

Given a set of words, I need to put them in an hash keyed on the first letter of the word. I have words = {}, with keys A..Z and 0 for numbers and symbols. I was doing something like

var firstLetter = name.charAt(0);
    firstLetter = firstLetter.toUpperCase();

if (firstLetter < "A" || firstLetter > "Z") {
    firstLetter = "0";
}
if (words[firstLetter] === undefined) {
    words[firstLetter] = [];
} 
words[firstLetter].push(name);

but this fails with dieresis and other chars, like in the word Ärzteversorgung. That word is put in the "0" array, how could I put it in the "A" array?

Upvotes: 5

Views: 32778

Answers (5)

adam.baker
adam.baker

Reputation: 1485

This is fortunately now possible without external libraries. Straight from the docs:

let story = "It’s the Cheshire Cat: now I shall have somebody to talk to.";

// Most explicit form
story.match(/\p{General_Category=Letter}/gu);

// It is not mandatory to use the property name for General categories
story.match(/\p{Letter}/gu);

Upvotes: 2

JDE
JDE

Reputation: 59

Try converting the character to its uppercase and lowercase and check to see if there's a difference. Only letter characters change when they are converted to their respective upper and lower case (numbers, punctuation marks, etc. don't). Below is a sample function using this concept in mind:

function isALetter(charVal)
{
    if( charVal.toUpperCase() != charVal.toLowerCase() )
       return true;
    else
       return false;
}

Upvotes: 5

jnrbsn
jnrbsn

Reputation: 2533

You could use a regular expression. Unfortunately, JavaScript does not consider international characters to be "word characters". But you can do it with the regular expression below:

var firstLetter = name.charAt(0);
firstLetter = firstLetter.toUpperCase();
if (!firstLetter.match(/^\wÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜäëïöüçÇßØøÅåÆæÞþÐð$/)) {
    firstLetter = "0";
}
if (words[firstLetter] === undefined) {
    words[firstLetter] = [];
} 
words[firstLetter].push(name);

Upvotes: 4

ajax333221
ajax333221

Reputation: 11764

You can use .charCodeAt(0); to get the position in the ASCII Chart and then do some checks.

The ranges you are looking for are probably 65-90, 97-122, 128-154, 160-165 (inclusive), but double check this by viewing the ASCII Chart

Something like this

if((x>64&&x<91)||(x>96&&x<123)||(x>127&&x<155)||(x>159&&x<166))

Where x is the Char Code

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

You can use this to test if a character is likely to be a letter:

var firstLetter = name.charAt(0).toUpperCase();
if( firstLetter.toLowerCase() != firstLetter) {
    // it's a letter
}
else {
    // it's a symbol
}

This works because JavaScript already has a mapping for lowercase to uppercase letters (and vice versa), so if a character is unchanged by toLowerCase() then it's not in the letter table.

Upvotes: 16

Related Questions