Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
197
rated 0 times [  198] [ 1]  / answers: 1 / hits: 36955  / 15 Years ago, mon, march 8, 2010, 12:00:00

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
}
}


Thank you for your time.


More From » jquery

 Answers
42

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?



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


[#97396] Friday, March 5, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yesseniadajab

Total Points: 258
Total Questions: 101
Total Answers: 127

Location: Mexico
Member since Mon, Sep 12, 2022
2 Years ago
;