Search code examples
javascriptregexregex-groupregex-look-ahead

Regex Matching to extract a text ending with space


Please help,

I need to extract a text from a long sentence

The text is as follows :

Current Balance: INR2,137,381.99 8/9/2020 Impaired normal

I would like to extract the amount from the above text and I used the regex

(?<=Current Balance:).+(?=/s)

And nothing getting..and I tried

(?<=Current Balance:).+(?=Impaired) and showing result : AED2,137,381.99 8/9/2020

So simply I want to note the text ending with space (the date is not static) , can anyone help on this?


Solution

  • Use

    /(?<=Current Balance:\s*)\S+/
    

    See proof. It matches one or more non-whitespace characters after Current Balance phrase, possibly followed with any amount of whitespace.

    const string = "Current Balance: INR2,137,381.99 8/9/2020 Impaired normal";
    const regex = /(?<=Current Balance:\s*)\S+/;
    console.log(string.match(regex)[0])

    Or, use capturing mechanism:

    /Current Balance:\s*(\S+)/
    

    JavaScript:

    const string = "Current Balance: INR2,137,381.99 8/9/2020 Impaired normal";
    const regex = /Current Balance:\s*(\S+)/;
    console.log(string.match(regex)[1])