Javascript extract info from regex

Docs: https://flaviocopes.com/javascript-regular-expressions/

Pattern test: https://regexr.com

Example

const body = 'Số dư tài khoản vừa tăng 100.000.000 vào 25/11/2019 14:41 VN\n' +
  'Số dư hiện tại: 1.389.944 VND\n' +
  'Mô tả: ssssssss FT1s99982\n' +
  'Số tài khoản: xxxxxxx’;

const bodyArr = body.split(/\r?\n/);
// Body must be 4 lines
if(bodyArr.length !== 4) return null;

const firstLineRegex = RegExp('^(Số dư tài khoản vừa tăng) (?<amount>[1-9][\\d+.]*) (vào) (?<paymentTime>\\d{2}\\/\\d{2}\\/\\d{4} \\d{2}:\\d{2} VN)$', 'g');
const firstLineData = firstLineRegex.exec(bodyArr[0]);
if(firstLineData === null || !firstLineData.groups) return null;
console.log(parseInt(firstLineData.groups['amount'].replace(/\./gm, '')));
console.log(firstLineData.groups['paymentTime']);

// 100.000
// 25/11/2019 14:41 VN

Leave a Reply

Your email address will not be published.Required fields are marked *