Search This Blog

2024/03/23

Javascript Regular Expression:Find all occurence of number preceeded by USD

Suppose we have given a string below


USD 100 USD 350 USD 345.56 USD currency USD rate

we need to find all occurances of number preceeded by USD

Code:
var line ="USD 100 USD 350 USD 345.56 USD currency USD rate"
var pattern=/USD(\s\d+\.{0,}\d+)/gm

var searchResult;
do {
searchResult = pattern.exec(line);
if (searchResult) {
console.log(searchResult[1]);
}
} while (searchResult);


Output:
100
350
345.56 100
350
345.56

No comments:

Post a Comment