Javascript 如何检查字符串是否仅包含拉丁字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14783196/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:54:30  来源:igfitidea点击:

How to check if string contains Latin characters only?

javascript

提问by PHP Noob

How to check if a string contains [a-zA-Z]characters only?

如何检查字符串是否[a-zA-Z]仅包含字符?

Example:

例子:

var str = '123z56';

回答by jondavidjohn

No jQuery Needed

不需要 jQuery

if (str.match(/[a-z]/i)) {
    // alphabet letters found
}

回答by Blender

You can use regex:

您可以使用正则表达式:

/[a-z]/i.test(str);

The imakes the regex case-insensitive. You could also do:

i使正则表达式不区分大小写。你也可以这样做:

/[a-z]/.test(str.toLowerCase());

回答by PHP Noob

Ahh, found the answer myself:

啊,我自己找到了答案:

if (/[a-zA-Z]/.test(num)) {
  alert('Letter Found')
}

回答by SomeoneYouDontKnow

There is no jquery needed:

不需要jquery:

var matchedPosition = str.search(/[a-z]/i);
if(matchedPosition != -1) {
    alert('found');
}

回答by Rick Mac Gillis

I'm surprised that the answers here got so many upvotes when none of them really answer the question. Here's how to make sure that ONLY LATIN characters are in a given string.

我很惊讶这里的答案得到了如此多的赞成,而他们都没有真正回答这个问题。以下是确保给定字符串中只有拉丁字符的方法。

const hasOnlyLetters = !!value.match(/^[a-z]*$/i);

The !!takes transforms something that's not boolean into a boolean value. (It's exactly the same as applying a !twice, and in fact you can use as many !as you'd like to toggle the truthiness multiple times.)

!!需要转换的东西,这不是布尔成一个布尔值。(这与应用 a!两次完全相同,实际上您可以使用任意数量的!次数来多次切换真实性。)

As for the RegEx, here's the breakdown.

至于正则表达式,这里是细分。

  • /.../iThe delimiter is a /and the imeans to assess the statement in a case-insensitive fashion.
  • ^...$The ^means to look at the very beginning of a string. The $means to look at the end of the string, and when used together, it means to consider the entire string. You can add more to the RegEx outside of these boundaries for things like appending/prepending a required suffix or prefix.
  • [a-z]*This part says to look for all lowercase letters. (The case-insensitive modifier means that we don't need to look at uppercase letters, too.) The *at the end says that we should match whats in the brackets any number of times. That way "abc" will match instead of just "a" or "b", and so forth.
  • /.../i定界符是 a/并且是以i不区分大小写的方式评估语句的方法。
  • ^...$^手段来看看一开始一个字符串。$查看字符串末尾的意思,当一起使用时,意味着考虑整个字符串。您可以在这些边界之外向 RegEx 添加更多内容,例如添加/添加所需的后缀或前缀。
  • [a-z]*这部分说要查找所有小写字母。(不区分大小写的修饰符意味着我们也不需要查看大写字母。)*最后说我们应该多次匹配括号中的内容。这样“abc”将匹配而不仅仅是“a”或“b”,等等。

回答by Jake Neumann

All these answers are correct, but I had to also check if the string contains other characters and Hebrew letters so I simply used:

所有这些答案都是正确的,但我还必须检查字符串是否包含其他字符和希伯来字母,所以我只是使用了:

if (!str.match(/^[\d]+$/)) {
    //contains other characters as well
}