Javascript简单的正则表达式不起作用 [英] Javascript simple regexp doesn't work
本文介绍了Javascript简单的正则表达式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码,它看起来不错,而且非常基本,但我不能让它工作:
I have this code, it looks alright and is really basic, but i can't make it work:
function checkValid(elem){
var abc = elem.value;
var re = "/[0-9]/";
var match = re.test(abc);
alert(match);
}
它匹配0和9,但不是1到8,这里有什么问题?谢谢。
It matches 0 and 9, but not 1 to 8, what's wrong here? Thanks.
推荐答案
使用\d匹配一个数字并使其成为常规表达式,而不是字符串:
Use \d to match a number and make it a regular expresison, not a string:
var abc = elem.value;
var re = /\d/;
var match = re.test(abc);
alert(match);
这篇关于Javascript简单的正则表达式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文