How can I tell if a string has any non-ASCII characters in it?

I’m looking to detect internationalized domain names and local portions in email addresses, and would like to know if there is a quick and easy way to do this with regex or otherwise in Javascript.

This should do it…

var hasMoreThanAscii = /^[\u0000-\u007f]*$/.test(str);

…also…

var hasMoreThanAscii = str
                       .split("")
                       .some(function(char) { return char.charCodeAt(0) > 127 });

ES6 goodness…

let hasMoreThanAscii = [...str].some(char => char.charCodeAt(0) > 127);

Try with this regex. It tests for all ascii characters that have some meaning in a string, from space 32 to tilde 126:

var ascii = /^[ -~]+$/;

if ( !ascii.test( str ) ) {
  // string has non-ascii characters
}

Edit: with tabs and newlines:

/^[ -~\t\n\r]+$/;

charCodeAt can be used to get the character code at a certain position in a string.

function isAsciiOnly(str) {
    for (var i = 0; i < str.length; i++)
        if (str.charCodeAt(i) > 127)
            return false;
    return true;
}

Simpler alternative to @alex’s solution:

const hasNonAsciiCharacters = str => /[^\u0000-\u007f]/.test(str);

You can use string.match() or regex.test() to achieve this. Following function will return true if the string contains only ascii characters.

string.match()

function isAsciiString(text) {
    let isAscii = true;
    if (text && !text.match(/^[\x00-\x7F]+$/g)) {
        isAscii = false;
    }
    return isAscii;
}  

regex.test()

function isAsciiString(text) {
    return /^[\x00-\x7F]+$/g.test(text);
} 

Example

console.log(isAsciiString("hello"));   // true
console.log(isAsciiString("hello©"));  // false


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   How to fix "bower ENOTFOUND"

Similar Posts