I'd like to remove whole string if the string inside brackets has the word ppm
. Simply remove whole bracket if the word ppm
inside. How do I do this?
"Menthol(0.3ppm)" ===> "Menthol"
"Michale(36 years old man)" ===> "Michale(36 years old man)"
"Good(5%, 5,500ppm)" ===> "Good"
What I tried
str.replace(/\(.+?ppm\)/gim, '')
It works if the word ppm
is located in end of brackets, but not working if ppm
is located in first or middle of string.
"abcd(333ppm)" ===> "abcd" (OK)
"abcd1234(abc 333ppm)" ===> "abcd1234" (OK)
"abcd(333ppm, 30%)" ===> "abcd(333ppm, 30%)" (NOT OK)
If there can not be (
or )
between the parenthesis, you could use a negated character class before and after ppm matching any char except the parenthesis or a newline.
\([^()\n]*ppm\b[^()]*\)
Explanation
\(
Match[^()\n]*
Match 0+ times any char except (
)
or newlineppm\b
Match ppm and word boundary[^()]*
Match 0+ times any char except (
)
or newline\)
Match )
[
"Menthol(0.3ppm)",
"Michale(36 years old man)",
"Good(5%, 5,500ppm)"
].forEach(s => console.log(s.replace(/\([^()\n]*ppm\b[^()]*\)/g, '')));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments