javascript 用于检测非 ascii 字符的 Java Script 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5185326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:17:55  来源:igfitidea点击:

Java Script regular expression for detecting non-ascii characters

javascriptregex

提问by sasidhar

How can we use java script to restrict the use of non-ascii characters in a specific text field..? thanks in advance...

我们如何使用 java 脚本来限制在特定文本字段中使用非 ascii 字符..?提前致谢...

回答by Juan Mendes

Ascii is defined as the characters in the range of 000-177 (octal), therefore

Ascii 定义为 000-177(八进制)范围内的字符,因此

function containsAllAscii(str) {
    return  /^[
function verify_url(txt){
    var str=txt.replace(/^\s*|\s*$/g,""); // remove spaces
    if (str == '') {
        alert("Please enter a URL for this page.");
        document.Form1.url.focus();
        return false;
    }
    found=/^[a-zA-Z0-9._\-]*$/.test(str); // we check for specific characters. If any character does not match these allowed characters, the expression evaluates to false
    if(!found) {
        alert("The can only contain letters a thru z, A thru Z, 0 to 9, the dot, the dash and the underscore. No spaces, German specific characters or Chinese characters are allowed. Please remove all punctuation (except for the dot, if you use it), and convert all non complying characters. In German, you may convert umlaut 'o' to 'oe', or in Chinese, you may use the 'pinyin' version of the Chinese characters.");
        document.Form1.url.focus();
    }
    return found;
}
0-7]*$/.test(str) ; }

http://jsfiddle.net/V5e4B/1/

http://jsfiddle.net/V5e4B/1/

You probably don't want to accept non-printing characters \000-\037, maybe your regex should be /\040-\0176/

您可能不想接受非打印字符\000-\037,也许您的正则表达式应该是/\040-\0176/

回答by Walter Pilimon

I came accross this page trying to look for a function to sanitize a string to be used as friendly URL in a CMS system. The CMS is multilingual but I wanted to prevent non-ascii characters to appear in the URL. So instead of using ranges, I simply used (based on the solution above):

我来到这个页面试图寻找一个函数来清理一个字符串,以便在 CMS 系统中用作友好的 URL。CMS 是多语言的,但我想防止非 ASCII 字符出现在 URL 中。因此,我没有使用范围,而是简单地使用了(基于上述解决方案):

##代码##