2
str = "33d4m"; //d for days and h for hours and m for min
patt=/^[1-9]+d/i;
result=patt.test(str);
document.write("Returned value: " +  result);

当且仅当 d 之前有一位数时,我希望结果返回 true,即;剩余不到 10 天或剩余几个小时,就像我希望返回 true 一样

str = "23h5m"  

如果 d 之前有两位数,则返回 false
如果 h 之前有两位数,则返回 true。
我哪里出错了。

4

3 回答 3

4

你可以试试这个:

patt=/^\d{1,2}h|^\dd/i

它的意思是:

   Match 1 or 2 digits followed by the literal 'h' 
OR match a single digit followed by the literal 'd'
于 2012-04-21T03:49:02.630 回答
1

我认为这样的事情会起作用:

patt=/^[1-9][dh]/i
于 2012-04-21T03:27:26.367 回答
1

加号表示“至少一个” - 删除它。您可能还想使用[0-9]所有数字,但这只是猜测。

patt=/^[1-9]d/i;
于 2012-04-21T03:27:36.183 回答