Getting individual parts of a regular expression in javascript match

I have the following regex in javascript. I want the string to match the string product (,|x) quantity, each price . The following regex does this; however, it returns an array with only one element. I want an array that returns the entire matched string first, and then the product, quantity, each price.

 // product,| [x] quantity, each price: var pQe = inputText.match(/(^[a-zA-z -]+)(?:,|[ x ]?) (\d{0,3}), (?:each) ([ยฃ]\d+[.]?\d{0,2})/g); 

(, | x) means a comma or x, so it will match ice-cream, 99, $99 or ice-cream x 99, $99

How can i do this?

+4
source share
2 answers

I believe this is what you are trying to do:

How to access mapped groups in a javascript regex?

Let me know if this helps.

+3
source

Use the exec function of the RegExp object. To do this, you will have to change the syntax to the following:

  var pQe = /(^[az ... {0,2})/g.exec(inputText) 

Element 1 of the returned array will contain the first element of the match, element 2 will contain the second, etc. If the string does not match the regular expression, it returns null.

+2
source

All Articles