Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 325
wwaawaw Avatar asked Nov 23 '12 03:11

wwaawaw


People also ask

How do I know if text is ASCII?

A simple browser-based utility that validates ASCII data. Just paste your ASCII text in the input area and you will instantly get the ASCII status in the output area. If the input contains only ASCII characters, you'll get a green badge, otherwise a red badge.

How do you find non Unicode characters?

To identify the Non Unicode characters we can use either Google Chrome or Mozilla firefox browser by just dragging and dropping the file to the browser. Chrome will show us only the row and column number of the .


2 Answers

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); 
like image 67
alex Avatar answered Sep 24 '22 15:09

alex


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]+$/; 
like image 21
elclanrs Avatar answered Sep 23 '22 15:09

elclanrs