wwa*_*waw 18 javascript regex unicode ascii
我希望检测电子邮件地址中的国际化域名和本地部分,并想知道是否有一种快速简便的方法来使用正则表达式或其他方式在Javascript中执行此操作.
ale*_*lex 24
这应该做到......
var hasMoreThanAscii = /^[\u0000-\u007f]*$/.test(str);
Run Code Online (Sandbox Code Playgroud)
...也...
var hasMoreThanAscii = str
.split("")
.some(function(char) { return char.charCodeAt(0) > 127 });
Run Code Online (Sandbox Code Playgroud)
ES6善良......
let hasMoreThanAscii = [...str].some(char => char.charCodeAt(0) > 127);
Run Code Online (Sandbox Code Playgroud)
elc*_*nrs 18
试试这个正则表达式.它测试所有在字符串中有一些含义的 ascii字符,从空格32到波浪号126:
var ascii = /^[ -~]+$/;
if ( !ascii.test( str ) ) {
// string has non-ascii characters
}
Run Code Online (Sandbox Code Playgroud)
编辑:使用标签和换行符:
/^[ -~\t\n\r]+$/;
Run Code Online (Sandbox Code Playgroud)
Nat*_*all 10
charCodeAt 可用于获取字符串中某个位置的字符代码.
function isAsciiOnly(str) {
for (var i = 0; i < str.length; i++)
if (str.charCodeAt(i) > 127)
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14439 次 |
| 最近记录: |