First of all, this is my first question in the community hence please pardon my wrongs Experts! I am learning regex and faced a scenario where I am failing to create answer by myself.
Let's say if there is humongous paragraph, can we first match on the basis of a specific suffix (say '%') and Only then go back and select the desired logic including suffix?
e.g. part of the text is "abcd efghMNP 0.40 % ijkl mnopSNP -3.20 % xyz". Now in this, if you notice - and I got this much - that there is pattern like
/([MS]NP[\s\d\.-%]+)/
I want to replace "MNP 0.40 %" or "SNP -3.20 %" with blank. replacing part seems easy :) But the problem is with all my learning I am not able to select desired ONLY IF there exists a '%' at the end of match.
The sequence of match I wish to reach at is -- if suffix '%' exists, then match the previous pattern, and if successful then select everything including suffix and replace with empty.
There are several expressions that would do so, for instance this one with added constraints:
[A-Z]{3}\s+[-+]?[0-9.]+\s*%
const regex = /[A-Z]{3}\s+[-+]?[0-9.]+\s*%/gm;
const str = `abcd efghMNP 0.40 % ijkl mnopSNP -3.20 % xyz
"MNP 0.40 %" or "SNP -3.20 %"`;
const subst = ``;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
Or a simplified version would be:
[A-Z]{3}(.*?)%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With