问题描述
仅当使用Javascript/jQuery包含英文字母和数字时,我才尝试查找某些文本.
Am trying to find some text only if it contains english letters and numbers using 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
}
}
谢谢您的时间.
推荐答案
正则表达式可能是:
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?
当您担心性能时,在对技术进行假设之前,请务必先进行一些简单的测试(除非您已经对该技术有深入的了解).
这篇关于Javascript查找是否仅英文字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!