Javascript查找是否仅英文字母 [英] Javascript find if english alphabets only

查看:116
本文介绍了Javascript查找是否仅英文字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当使用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?

编辑,我的大脑过滤掉了我不想使用正则表达式",因为它未通过"isSilly()"测试.您始终可以检查单词中每个字母的字符代码,但这要比让正则表达式匹配器工作起来要慢(可能很多要慢).内置的正则表达式引擎非常快.

edit my 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 much slower) than letting the regex matcher work. The built-in regular expression engine is really fast.

当您担心性能时,在对技术进行假设之前,请务必先进行一些简单的测试(除非您已经对该技术有深入的了解).

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).

这篇关于Javascript查找是否仅英文字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆