Home > Back-end >  regex 2 numbers from a string
regex 2 numbers from a string

Time:10-18

How can I pull out the 2 numbers in-between Price Our: and the following | from this string?

"Delivery: Price => 30.00 - 45.00 | Price Our: 1900.00 => 1800.00 | Delivery: Contact => 3 - 4 Days | "

I tried (Price Our:.*?(?=\|)) But thats the limit of my regex knowledge

CodePudding user response:

You could use 2 capture groups:

\bPrice Our: (\d (?:\.\d )?) => (\d (?:\.\d )?) \|

The pattern matches:

  • \bPrice Our: A word boundary to prevent a partial match, then match Price Our:
  • (\d (?:\.\d )?) Capture group 1, match a number with an optional decimal part
  • => Match literally
  • (\d (?:\.\d )?) Capture group 2, match a number with an optional decimal part
  • \| Match |

enter image description here

const regex = /(\d*\.\d )/gm;
const str = "Delivery: Contact => 3 - 4 Days | Price Our: 1900.00 => 1800.00 | Delivery: Contact => 3 - 4 Days | ";

const res = str.match(regex)

console.log(res)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related