简体   繁体   English

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

[英]How to check if string contains Latin characters only?

How to check if a string contains [a-zA-Z] characters only?如何检查字符串是否仅包含[a-zA-Z]字符?

Example:例子:

var str = '123z56';

No jQuery Needed不需要 jQuery

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

You can use regex:您可以使用正则表达式:

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

The i makes the regex case-insensitive. i使正则表达式不区分大小写。 You could also do:你也可以这样做:

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

Ahh, found the answer myself:啊,我自己找到了答案:

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

There is no jquery needed:不需要jquery:

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

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. take 将非布尔值转换为布尔值。 (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.) (这与应用!两次完全相同,事实上,您可以使用任意数量的!多次切换真实性。)

As for the RegEx, here's the breakdown.至于正则表达式,这里是细分。

  • /.../i The delimiter is a / and the i means to assess the statement in a case-insensitive fashion. /.../i分隔符是/ ,而i表示以不区分大小写的方式评估语句。
  • ^...$ 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.您可以在这些边界之外向 RegEx 添加更多内容,例如添加/添加所需的后缀或前缀。
  • [az]* This part says to look for all lowercase letters. [az]*这部分表示查找所有小写字母。 (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.这样“abc”将匹配而不仅仅是“a”或“b”,等等。

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
}

The answers above don't really seem to answer your question.上面的答案似乎并没有真正回答你的问题。 A function that returns true for a latin only string and false else, could look like this:返回函数true的拉丁唯一字符串和false东西,看起来是这样的:

function latinOnly(str) {
    var matchStr = str.match(/[a-zA-Z]+/);
    if (matchStr == null) {
        return false;
    }
    return matchStr[0].length == str.length;
}

str.match(/[a-zA-Z]+/)[0] will return the first continous subsequence on latin characters only. str.match(/[a-zA-Z]+/)[0]将仅返回拉丁字符的第一个连续子序列。 For example:例如:

"test123test" -> "test"
"%$haG#=ß9test" -> "haG"
";9893rtrt" -> "rtrt"
"(/(43" -> null (has to be catched)
"hello" -> "hello"

Therefore, if there are only latin characters in the string, the mtach result will be the same string.因此,如果字符串中只有拉丁字符,则 mtach 结果将是相同的字符串。 So if the input string and the match string have the same length, you know that it contained latin characters only.因此,如果输入字符串和匹配字符串的长度相同,您就知道它只包含拉丁字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM