art
art

Reputation: 306

Fetching multiple itemlist using regex

The below code is used to fetch item list from order_details

Output expected multiple items enter image description here

Below code works fine for 1 ITEM but the 2nd item/multiple items are not fetched using the below regex.

I want it to be dynamic so that it should work for any number of the item found in the matching pattern.

 var order_details = "Detalle\n Unidad\n Cantidad\n Total\n     Triple Insomnio Especial   S/37 1  S/37.90  Insomnio Especial  S/21 2 S/23.12 *Tipo de papa:*  - Papa Amarilla Tumbay S/0.00\n     *Adicional :*  - Quesp cheddar S/0.00\n     *Agrega Bebida:*  - Fanta 400 ml S/0.00\n     *Salsas:*  - Mayonesa S/0.00\n - Ketchup S/0.00\n - Golf S/0.00\n - Guacamole S/0.00\n - Ají S/0.00\n - Tártara S/0.00\n - Mayo-aji S/0.00\n      "     ;
    
    var rx = /\s+(.*?)\s+(S\/.*?)\s+(\d+)\s+(S\/.*?)\s+([\s\S]*)/
    
    var m = rx.exec(order_details)
    
    var Detalle = m[1]
    var Unidad = m[2]
    var Cantidad = m[3]
    var Total = m[4]
    var Other = m[5]
    
    console.log(Detalle)
    console.log(Unidad)
    console.log(Cantidad)
    console.log(Total)
    console.log(Other)

Upvotes: 0

Views: 50

Answers (1)

The Scientific Method
The Scientific Method

Reputation: 2436

You may try this one,

(?:\s+?).+?\s+(S\/\d+)\s+(\d+)\s+(S\/\d+\.\d+)

try demo here

Upvotes: 1

Related Questions