0

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)

11
  • your string is not proper, new lines should be for each row right? why did new line came at "Insomnio Especial \nS/212" ? Commented Sep 9, 2018 at 14:16
  • string comes from the web so I have no control over it that's why I have to write a regex which will work for the case, even if we remove \n from Insomnio Especial \nS/212 the above regex doesnt work @TilakPutta Commented Sep 9, 2018 at 14:19
  • regex wont work if we remove \n .. but you can split once by \n and then by \t to get it as rows and columns Commented Sep 9, 2018 at 14:21
  • can you post exact string that is generated by web Commented Sep 9, 2018 at 14:22
  • exact string I have posted @TilakPutta Commented Sep 9, 2018 at 14:25

1 Answer 1

1

You may try this one,

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

try demo here

Sign up to request clarification or add additional context in comments.

2 Comments

output id different as expected only 2 items should have been fetched , like in the output image @The Scientific Method
@art check it now, it is fetching two rows only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.