Advertisement
ggeorgiev88

regexp

Mar 22nd, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function adAstra(data) {
  2.  
  3.     let regex = /([\#\|])(?<product>[A-Z a-z]+)\1(?<date>[\d]{2}\/[\d]{2}\/[\d]{2})\1(?<calories>[\d]+)\1/g
  4.     let totalCalories = 0
  5.     while (regex.test(data)) {
  6.         let currLine = regex.exec(data);
  7.         let product = currLine.groups.product;
  8.         let date = currLine.groups.date;
  9.         let calories = Number(currLine.groups.calories);
  10.         console.log(product)
  11.         console.log(date)
  12.         console.log(calories)
  13.        
  14.  
  15.  
  16.     }
  17. }
  18.  
  19. adAstra(['#Bread#19/03/21#4000#|Invalid|03/03.20||Apples|08/10/20|200||Carrots|06/08/20|500||Not right|6.8.20|5|'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement