最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript RegEx Exactly one Digit Pattern - Stack Overflow

programmeradmin6浏览0评论
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);

I want result return true if and only if there is one digit before d, i.e;less than 10days remaining or a few hours remaining like i want return true also on

str = "23h5m"  

if two digit before d then return false
if two digit before h then return true.
Where i am going wrong.

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

I want result return true if and only if there is one digit before d, i.e;less than 10days remaining or a few hours remaining like i want return true also on

str = "23h5m"  

if two digit before d then return false
if two digit before h then return true.
Where i am going wrong.

Share Improve this question edited Apr 21, 2012 at 3:40 Wasim A. asked Apr 21, 2012 at 3:24 Wasim A.Wasim A. 9,87622 gold badges94 silver badges121 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You could try this:

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

It means:

   Match 1 or 2 digits followed by the literal 'h' 
OR match a single digit followed by the literal 'd'

The plus means "at least one" - remove it. You may also want to use [0-9] for all digits, but that is just a guess.

patt=/^[1-9]d/i;

i think something like this would work:

patt=/^[1-9][dh]/i
发布评论

评论列表(0)

  1. 暂无评论