MathieuAuclair
MathieuAuclair

Reputation: 1337

Javascript encoding checker ASCII/UFT8

Is there a way to replicate mb_detect_encoding from PHP with javascript? I'm trying to recreate something like this.

 isAscii("éton") //false
 isAscii("hello") //true

Or is there a way to check if a string has already been encoded with utf-8?

Upvotes: 1

Views: 929

Answers (1)

Luca Kiebel
Luca Kiebel

Reputation: 10096

Since all ASCII characters are inbetween \x00 and \x7F you could simply test if your string matches that range of characters with a RegEx.

The RegEx would be /^[\x00-\x7F]*$/, and using the //.test() method returns whether or not the input fits the pattern:

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

console.log(isAscii("éton")) //false
console.log(isAscii("hello")) //true

as @duskwuff points out, it would be more performant to check if the string contains any characters not from the ASCII set and then return the opposite of that result:

function isAscii(str) {
  return !/[^\x00-\x7f]/.test(str);
}

console.log(isAscii("éton")) //false
console.log(isAscii("hello")) //true

Note however that this is not encoding, it's which set the characters are from.

Upvotes: 3

Related Questions