1

I'm making a Chrome extension that searches a page for a dollar amount (a number with no more then two decimal places immediately preceded by a "$") then tacks on a bit with how much that value would be in another currency. I found a commonly used regex that matches exactly those parameters.

/^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$/g

so I'm thinking I have a nice headstart. I've only been coding a couple of months and of all the concepts I've encountered, regex's give me the most headache. I test out my shiny new expression with:

var regex = /^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$/g;

var str = "The total it $2.25 Would you like paper or plastic?";

r = regex.test(str);
console.log(r);

and of course that sucker returns false! I tried a few more strings with "2.25" or "$2" or "$2.256" just to be sure and they all returned false.

I am thoroughly stumped. The expression came recommended, I'm using .test() correctly. All I can think of is it's probably some small newbish detail that has nothing to do with regex's.

Thanks for your time.

  • That regex looks like it matches at the start of the string, so it would only match if you tested it on ONLY a dollar value (without the rest of the string). My regex-fu isn't strong enough to fix that, but that's somewhere to start (the `^` at the start of the regex matches the start of the string). – Chris O'Kelly Dec 02 '15 at 22:34
  • Hint, it's the `^` --> https://regex101.com/r/cA1tE9/1 – Jonathan Dec 02 '15 at 22:35
  • The `$` at the end of the regex is also a problem – nderscore Dec 02 '15 at 22:35
  • Well, the last `$2.256` [is not matched](https://regex101.com/r/qW3sT8/1). Not sure it is [that dupe I chose](http://stackoverflow.com/questions/34034770/javascript-regexp-test-method-weird-behaviour). At any rate, you must remove the `/g` flag if you want to use the regex with `RegExp#test()`. – Wiktor Stribiżew Dec 02 '15 at 22:36
  • Yeah, I don't think this is a valid duplicate. – nderscore Dec 02 '15 at 22:42

1 Answers1

1

Your overly complex regular expression is checking the entire string. Remove the ^ and $ which denote the beginning and end of the string, respectively. Then remove the /g flag, which is used to search for multiple matches.

What's wrong with checking for /\$\d+\.\d\d/?

I find http://regex101.com/ to be a helpful resource.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • 1
    Thank you. I thought the ^ and \$ were combined and it meant "start looking for everything else that begins with a $". I didn't even see that last $. I just discovered regex101 today while working on this problem. I'll have to spend some serious time with it. – Wes Schumaker-Reid Dec 02 '15 at 23:09
  • One thing though. Your expression workswonderfully for sites like amazon but if someone just posts "$35" with no cents it still returns false which is enough to get started but I'll have to work something out later. – Wes Schumaker-Reid Dec 02 '15 at 23:17
  • 2
    `/\$\d+(?:\.\d\d)?/` makes the decimal portion optional – Chris O'Kelly Dec 02 '15 at 23:20