Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression question 3

Status
Not open for further replies.

brianpercival

Programmer
Jun 13, 2005
63
US
I am testing a single digit number for the regular expression re1=/^\d+\.?\d{2}$/ and it fails. any thing greater than 2 digits, it works. Why is that?

Basically thisis supposed to be a check for a positive decimal number.

I want it to consider just integers too. What should I change it to?

regards,
Brian
 
Taking your regular expression:
Code:
/^\d+\.?\d{2}$/
And breaking it into normal-speak...

^ from the start of the input
\d+ match one (or more) digits
\. match a literal .
? match it zero or one times
\d{2} match exactly 2 digits
$ to the end of the input

The regexp is only allowed to match: One or more digits, followed by an optional . and then exactly two digits.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
any thing greater than 2 digits, it works. Why is that?

Let's break apart the regex: /^\d+\.?\d{2}$/
1: \d+
2: \.?
3: d{2}

What this does is (1) look for at least 1 or more instances of a number, (2) 0 or more instances of a decimal, (3) and then 2 numbers.

So if we broke it down to bare minimums - it will look for 1 number, 0 decimals, and then 2 more numbers = a 3 digit number.

Try this instead - it will take an integer, or a number with only 2 decimal places (I'm guessing that's what you were trying to achieve)

Code:
<script language="javascript">
function blah(str) {
   if (/^\d+(\.\d{2})?$/.test(str)) {
      alert("regex passed");
      return true;
   }
   alert("regex failed");
   return false;
}
</script>
<input type="text" id="blahText"><br>
<input type="button" value="check regex" onclick="blah(document.getElementById('blahText').value)">

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Wow, judging by time of posts it took me more than 10 mins to write that up.... terrible

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Took me about 16 minutes (regular expressions are still something I have to work on myself)... so you're still faster than me (using some form of weird maths) [smile]

Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Funny that we had almost identical posts - great minds........

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
kaht,

Thanks a lot. I see now what I was doing wrong. I was wondering how come no one replied sooner this time.. :) But this forum is the fastest as far as I know.

regards,
Brian
 
I was wondering how come no one replied sooner this time.
RegExp is more of an added bonus for javascript (and other languages). You can learn javascript, and be awesome at javascript, and never have to touch a regular expression. Anything you can do with a regular expression do can do with regular javascript code. For this reason many ppl don't learn how to use them. Once you do though, they can cut many many many many lines of code down to one simple statement, which is why I love them so much.

P.S. I gave you a star too jeff, our posts had the same info pretty much.

P.S.S. You can give out more than 1 star per post if more than one person helps you.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
what's up with this star thing? Does someone count them all at the end of the day/month etc?

Brian
 
Well... stars have no intrinsic value. They do show your appreciation in a public way - and they can indicate that a thread is worth taking a look at (often the more stars, the more chance I have of reading the thread when I'm browsing).

Anyway, thanks for the appreciation (both of you)... a nice way to wind up Friday afternoon (for me anyway).

Have a great weekend!
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
what's up with this star thing? Does someone count them all at the end of the day/month etc?
It determines the tip-master of the month. Additionally, it keeps rankings in each forum. If you direct your attention to the column on the right at the top of the page you'll see the javascript MVP list. # of stars in this forum also affects that. Some don't care, for some it's the only reason they post replies. I think of it as a friendly competition among friends (I see all the common posters here on a daily basis, I consider them friends).

It's a nice way to say "thank you" for help given, and I think it's one of the reasons that people are quick to respond here, and also the reason that it attracts so many professionals.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top