简体   繁体   English

JavaScript 和正则表达式:如何检查字符串是否仅为 ASCII?

[英]JavaScript & regex : How do I check if the string is ASCII only?

I know I can validate against string with words ( 0-9 AZ az and underscore ) by applying W in regex like this:我知道我可以通过在正则表达式中应用W来验证带有单词(0-9 AZ az 和下划线)的字符串,如下所示:

function isValid(str) { return /^\w+$/.test(str); }

But how do I check whether the string contains ASCII characters only?但是如何检查字符串是否仅包含 ASCII 字符? ( I think I'm close, but what did I miss? ) (我想我已经接近了,但我错过了什么?)

Reference: https://stackoverflow.com/a/8253200/188331参考: https : //stackoverflow.com/a/8253200/188331

UPDATE : Standard character set is enough for my case.更新:标准字符集对我来说就足够了。

All you need to do it test that the characters are in the right character range.您只需要测试字符是否在正确的字符范围内即可。

function isASCII(str) {
    return /^[\x00-\x7F]*$/.test(str);
}

Or if you want to possibly use the extended ASCII character set:或者,如果您想使用扩展的 ASCII 字符集:

function isASCII(str, extended) {
    return (extended ? /^[\x00-\xFF]*$/ : /^[\x00-\x7F]*$/).test(str);
}

You don't need a RegEx to do it, just check if all characters in that string have a char code between 0 and 127:您不需要 RegEx 来执行此操作,只需检查该字符串中的所有字符是否都具有介于 0 和 127 之间的字符代码:

function isValid(str){
    if(typeof(str)!=='string'){
        return false;
    }
    for(var i=0;i<str.length;i++){
        if(str.charCodeAt(i)>127){
            return false;
        }
    }
    return true;
}

For ES2018, Regexp support Unicode property escapes , you can use /[\\p{ASCII}]+/u to match the ASCII characters.对于 ES2018,Regexp 支持Unicode 属性转义,您可以使用/[\\p{ASCII}]+/u来匹配 ASCII 字符。 It's much clear now.现在已经很清楚了。

Supported browsers:支持的浏览器:

  • Chrome 64+铬 64+
  • Safari/JavaScriptCore beginning in Safari Technology Preview 42 Safari/JavaScriptCore 从 Safari Technology Preview 42 开始
var check = function(checkString) {

    var invalidCharsFound = false;

    for (var i = 0; i < checkString.length; i++) {
        var charValue = checkString.charCodeAt(i);

        /**
         * do not accept characters over 127
         **/

        if (charValue > 127) {
            invalidCharsFound = true;
            break;
        }
    }

    return invalidCharsFound;
};

暂无
暂无

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

相关问题 如何检查字符串中是否包含 ASCII 字母? - How do I check if a string has an ASCII letter in it? 如何从JavaScript中的字符串中获取ASCII代码? - How do I get an ASCII code from a string in JavaScript? 如何在javascript中检查字符串是否只包含数字,逗号和带正则表达式的句点? - How can I check if a string contains only numbers, comma and periods with regex in javascript? Javascript-如何检查正则表达式以确保字符串至少包含一个字母/数字序列? - Javascript - how do I check regex to ensure a string contains at least one sequence of letters/numbers? 我如何使用javascript数组检查字符串匹配的正则表达式是否已经存在 - How do I check if a string matching regex already exists in array with javascript 在Javascript中,如何检查字符串是否只是字母+数字(允许下划线)? - In Javascript, how do I check if string is only letters+numbers (underscore allowed)? 如何使用正则表达式进行一些Javascript字符串操作? - How do I use regex to do some Javascript string manipulation? 如何仅替换以下正则表达式(JavaScript)的一部分? - How do I replace only a portion of the following regex (JavaScript)? javascript-如何使用正则表达式匹配以下字符串? - javascript - How do I use regex to match the following string? 如何在JavaScript中的字符串中间进行锚定的正则表达式匹配 - How do I make an anchored regex match in the middle of a string in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM