-2

The text I would like to parse always comes in the following pattern:

"Your $22.12 transaction with Amazon.com"

I would like to parse all text after "transaction with " which would be the name of company/store.

Can anybody help with this? Thank you so much!


This question does not have a relevant enough answer here : Javascript Regexp - Match Characters after a certain phrase That question is about matching characters after a fixed, exact, constant phrase. The phrase in this question contains a substring (the $ amount) that varies. An answer should explain, in addition to how to get characters after text, how to match a dollar money amount with RegEx.

clickbait
  • 2,818
  • 1
  • 25
  • 61
  • So what have you tried and what was your problem? Was your problem finding the correct RegExp? Was it about using the RegExp? … – t.niese Mar 19 '22 at 17:58

1 Answers1

0

This Regular Expression may work:

Your \$\d+\.\d{2} transaction with (.*)

Just make a regex that matches the beginning phrase followed by (.*).

\d matches any digit character from 0 to 9.

The plus sign + matches 1 or more of what precedes it. \d+ matches 1 or more digits.

{2} matches exactly 2 of something. \d{2} matches 2 digits.

. matches any character. * matches zero or more of what comes before it. So Regex .* matches any number of any character, greedily. Wrapping .* in parentheses creates a capture group that lets you get the matched text after running the regex on it.

const txt = 'Your $22.12 transaction with Amazon.com'
const regEx = /Your \$\d+\.\d{2} transaction with (.*)/
const matches = txt.match(regEx)
document.write(matches[1])
clickbait
  • 2,818
  • 1
  • 25
  • 61