Remove everything contained by a pair of parenthesis including the parenthesis itself in case they enclose a specific substring

JuntaeKim

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)
The fourth bird

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 newline
  • ppm\b Match ppm and word boundary
  • [^()]* Match 0+ times any char except ( ) or newline
  • \) Match )

Regex demo

[
  "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.

edited at
0

Comments

0 comments
Login to comment

Related

Replacing with sed a string containing parenthesis with a substring of contained date into the parenthesis

How remove a specific substring between parenthesis using Regular Expressions?

Capture everything enclosed by parenthesis

Regex multiple parenthesis and remove one with specific pattern

Find the parenthesis pair () within a list of parenthesis

How to remove parenthesis and everything in between in a string in R where the parenthesis could be anywhere in the string?

How do you remove everything within parenthesis in BQSQL?

Remove parenthesis and contents in parenthesis if present in a df column

Will be executed everything which is into parenthesis first?

Regex - everything but anything within parenthesis

Remove optional last parenthesis

Remove parenthesis and Characters inside it

How to remove unnecessary parenthesis?

Remove parenthesis with jQuery

Remove parenthesis in postgresql loop

Remove text and parenthesis in Excel?

Remove parent parenthesis with regex

Values inside specific parenthesis

How can I remove a parenthesis that begins with a specific string using python?

How to delete everything inside a parenthesis without deleting the parenthesis in Vim?

regex to remove nested parenthesis brackets

remove parenthesis from a list of lists

Python and regex to remove parenthesis in a file

Remove parenthesis if length > max length

How to remove the parenthesis and a comma in the output

angularjs - remove currency filter parenthesis

Remove last occurrence of Parenthesis PHP

remove empty parenthesis from expression

Remove Redundant Parenthesis in an Arithmetic Expression