wwaawaw
wwaawaw

Reputation: 7127

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.

Upvotes: 29

Views: 30159

Answers (5)

Ruchira Nawarathna
Ruchira Nawarathna

Reputation: 1477

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

Upvotes: 0

Munawwar
Munawwar

Reputation: 1852

Simpler alternative to @alex's solution:

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

Upvotes: 1

alex
alex

Reputation: 490153

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);

Upvotes: 33

elclanrs
elclanrs

Reputation: 94101

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]+$/;

Upvotes: 24

Nathan Wall
Nathan Wall

Reputation: 10614

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;
}

Upvotes: 11

Related Questions