kecman
kecman

Reputation: 853

RegEx for detecting brackets whose content begins with two possible strings

I need a regex to detect these two types of string:

(+US$[ANYTHING GOES HERE])

(-US$[ANYTHING GOES HERE])

So for example, these are valid:

(+US$5.50)
(-US$8892323.45)

I have found a regex for identifying brackets, and it looks like this, but I'm not sure how to change it so it detects only brackets whose content begins with "+$US" or "-$US"

/\((.*?)\)/

Upvotes: 1

Views: 29

Answers (1)

gyre
gyre

Reputation: 16787

Try using /^\([+-]US\$\d+(?:\.\d{2})?\)/:

console.log([
  '(+US$5.50)', //=> true
  '(-US$8892323.45)', //=> true
  '(+US$5.50) STUFF AFTER MATCH', //=> false
  '-US$8892323.45' //=> false
].map(/ /.test,
  /^\([+-]US\$\d+(?:\.\d{2})?\)$/
))

Upvotes: 1

Related Questions