Javascript Javascript查找是否仅英文字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2402886/
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-08-23 00:09:10  来源:igfitidea点击:

Javascript find if english alphabets only

javascriptjquery

提问by Alec Smart

Am trying to find some text only if it contains english letters and numbers using Javascript/jQuery.

我正在尝试使用 Javascript/jQuery 查找包含英文字母和数字的文本。

Am wondering what is the most efficient way to do this? Since there could be thousands of words, it should be as fast as possible and I don't want to use regex.

想知道最有效的方法是什么?由于可能有数千个单词,因此应该尽可能快,而且我不想使用正则表达式。

 var names[0] = 'test';
 var names[1] = '???';
 var names[2] = '??????';

 for (i=0;i<names.length;i++) {
    if (names[i] == ENGLISHMATCHCODEHERE) {
        // do something here
    }
 }

Thank you for your time.

感谢您的时间。

回答by Pointy

A regular expression for this might be:

一个正则表达式可能是:

var english = /^[A-Za-z0-9]*$/;

Now, I don't know whether you'll want to include spaces and stuff like that; the regular expression could be expanded. You'd use it like this:

现在,我不知道您是否想要包含空格之类的东西;正则表达式可以扩展。你会像这样使用它:

if (english.test(names[i])) // ...

Also see this: Regular expression to match non-English characters?

另请参阅:Regular expression to match non-English characters?

editmy brain filtered out the "I don't want to use a regex" because it failed the "isSilly()" test. You could always check the character code of each letter in the word, but that's going to be slower (maybe muchslower) than letting the regex matcher work. The built-in regular expression engine is really fast.

编辑我的大脑过滤掉了“我不想使用正则表达式”,因为它没有通过“isSilly()”测试。您可以随时在单词的每个字母的字符代码,但是这将是慢(也许很多不是让正则表达式匹配的工作慢)。内置的正则表达式引擎非常快。

When you're worried about performance, always do some simple tests first before making assumptions about the technology (unless you've got intimate knowledge of the technology already).

当您担心性能时,总是先做一些简单的测试,然后再对技术做出假设(除非您已经对技术有了深入的了解)。

回答by T.J. Crowder

If you're dead set against using regexes, you coulddo something like this:

如果您坚决反对使用正则表达式,则可以执行以下操作:

// Whatever valid characters you want here
var ENGLISH = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

function stringIsEnglish(str) {
    var index;

    for (index = str.length - 1; index >= 0; --index) {
        if (ENGLISH.indexOf(str.substring(index, index + 1)) < 0) {
            return false;
        }
    }
    return true;
}

...but a regex would almost certainly be orders of magnitude faster.

...但正则表达式几乎肯定会快几个数量级

回答by raveren

Using regex is the fastest way to do this I'm afraid. This to my knowledge should be the fastest algorithm:

恐怕使用正则表达式是最快的方法。据我所知,这应该是最快的算法:

var names = 'test',
var names[1] = '???';
var names[2] = '??????';

//algorithm follows
var r = /^[a-zA-Z0-9]+$/,
    i = names.length;

while (--i) {
    if (r.test(names[i])) {
        // do something here
    }
}

回答by mhd196

You should consider words that may contain special characters. For example {it's}, isn't it english?

您应该考虑可能包含特殊字符的单词。例如{it's},不是英文吗?

回答by vsync

var string = "aAzZ123dsfsdf???????";

function englishString(s){
    var i, charCode;
    for (i = s.length; i--;) {
        charCode = s.charCodeAt(i);
        if (charCode < 65 || charCode > 122) 
            return false;
    }
    return true;
}

englishString(string); // false
englishString('abxSDSzfgr'); // true