Javascript 检测字符是否为字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10707972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting if a character is a letter
提问by cdarwin
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
给定一组单词,我需要将它们放在以单词首字母为键的散列中。我有单词 = {},键 A..Z 和 0 代表数字和符号。我在做类似的事情
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?
但这会因 dieresis 和其他字符而失败,例如?rzteversorgung。这个词放在“0”数组中,我怎么能把它放在“A”数组中?
回答by Niet the Dark Absol
You can use this to test if a character is likely to bea 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.
这是有效的,因为 JavaScript 已经有小写到大写字母的映射(反之亦然),所以如果一个字符到toLowerCase()
那时没有改变,它就不会出现在字母表中。
回答by jnrbsn
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:
您可以使用正则表达式。不幸的是,JavaScript 并不将国际字符视为“单词字符”。但是你可以用下面的正则表达式来做到这一点:
var firstLetter = name.charAt(0);
firstLetter = firstLetter.toUpperCase();
if (!firstLetter.match(/^\wàèìòùàèìòùáéíóúYáéíóúy?ê???aê?????????????ü????ü?????????TtDe$/)) {
firstLetter = "0";
}
if (words[firstLetter] === undefined) {
words[firstLetter] = [];
}
words[firstLetter].push(name);
回答by JDE
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;
}
回答by ajax333221
You can use .charCodeAt(0);
to get the position in the ASCII Chart and then do some checks.
您可以使用.charCodeAt(0);
获取 ASCII 图表中的位置,然后进行一些检查。
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
您要查找的范围可能是 65-90、97-122、128-154、160-165(含),但请通过查看ASCII 图表仔细检查
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
x
字符代码在哪里
回答by adam.baker
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);