简体   繁体   中英

How to check if string contains Latin characters only?

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

Example:

var str = '123z56';

No jQuery Needed

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. 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:

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. (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.
  • ^...$ 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.
  • [az]* 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.

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:

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. 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. So if the input string and the match string have the same length, you know that it contained latin characters only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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