-3

Here is my values:

Account Status: Open
Code: 1632585 Status: Open

Im trying to create regex that will find on the list "Account Status" if will search for "Account Status" and its easy, but when Im trying to get Status from 2nd line - searching by "Status", it returns me first occurrence - "Account Status", but I want Status from line where Code is. Im looking for Regex for this case, but I cant solve it.

gagatek
  • 65
  • 1
  • 13

1 Answers1

1

If you know that the format will be always the same (number before the status) you could do something like:

function getAccountStatus(string) {
    let regex = /Account\s*Status:\s*(\w+)/;
    return regex.exec(string)[1];
}
function getStatus(string) {
    let regex = /[\d]\s*Status:\s*(\w+)/;
    return regex.exec(string)[1];
}

let string1 = 'Account Status: Open\nCode: 1632585 Status: Open';
console.log('String1: ' + string1.replace('\n', ' / '));
console.log('Account Status (string1): ' + getAccountStatus(string1));
console.log('Status (string1): ' + getStatus(string1));

let string2 = 'Account Status: Closed\nCode: 1632585 Status: Open';
console.log('String2: ' + string2.replace('\n', ' / '));
console.log('Account Status (string2): ' + getAccountStatus(string2));
console.log('Status (string2): ' + getStatus(string2));
jeprubio
  • 14,400
  • 5
  • 32
  • 47
  • Ok, I found regex, maybe it's not the prettiest solution but it works!:) `(\\d.*${key}.*)|^\\s*${key}.*` – gagatek Mar 18 '20 at 11:48