繁体   English   中英

JavaScript正则表达式的麻烦

[英]Trouble with JavaScript Regular Expression

我有一个棘手的时间让这个正则表达式工作。 我到目前为止的模式是:

var dollarPattern = /^\d{1,}|\s\d/gi;

var matchedResults = new Array();
matchedResults = textValue.match(dollarPattern);

我希望实现的是使用示例字符串“2到2.99(63项)”,我想检查它是以数字开头,还是包含空格后跟数字(在这种情况下,两个条件)是真的)。 但是,我不断在Firefox中遇到“matchedResults为null”错误(尽管它的长度应为2)。

我有什么想法我做错了吗? 谢谢...

你所需要的只是逃避。

// Please note that this works, but @Lekensteyn pointed out that this uses string literals
// Also, this will give you the first match(if exists) only.
alert("2 to 3.99 (63 items)".match("^\\d\|\\s\\d"));

要么

// This one will give you all possible matches
alert("2 to 3.99 (63 items)".match(/^\d|\s\d/g));

适用于Chrome,FF4和IE8。

当我在正则表达式周围加上引号时,我得到null。

但是,输出我得到它“2,2”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM