Home > Enterprise >  How to remove all parenthesis and their contents from a string using a regular expression or pure Ja
How to remove all parenthesis and their contents from a string using a regular expression or pure Ja

Time:01-08

Currently I am entering text inside a text area and on clicking the submit button of my form, I am applying some changes to the text mainly using .replaceAll(); js methods and printing it out as a paragraph inside a div.

If I enter text that already has newlines as well as parenthesis into the text area, I want my script to strip/remove all parenthesis as well as the contents inside parenthesis AND keep all newlines as they are.

I have tried the following regex js function with the sample text however there are some formatting issues that get introduced (some newlines are removed and text following parenthesis ends up on the same line as the text in the previous line where parenthesis was removed)

regex js code:

updatedText = updatedText.replaceAll(/\s*\(.*?\)\s*/g, '');

Sample input:

06.abbaa(bbbbadf)
01.lijer      ttt
06.foo(sample)dddddddddd(garbage)
06.text(lksdhfljkld)20001(kkkkkksssss)
01.asdfg
13.fffffffff(fdsgggg/asdfasdf)
03.ggggggg(defg/abc)
18.abc     123abc
blahblah
qqqqqq
anon
06.barbarbar
10.foobar
18.fooistbar
12.blahhhh(moreblah/blah)
15.2035number(test)(test0/2test)
12.testing morewords
03.random(blahhh)(blahahaha/morerandomstuff)
17.anotherrandomtextstring
11.string
testy(mctest)
isTestingStrings
11.string
11.string
11.string

the result outputted where lines are combined when they are not suppose to be is this:

06.abbaa01.lijer ttt
06.foodddddddddd06.text2000101.asdfg
13.fffffffff03.ggggggg18.abc 123abc
blahblah
qqqqqq
anon
06.barbarbar
10.foobar
18.fooistbar
12.blahhhh15.2035number12.testing morewords
03.random17.anotherrandomtextstring
11.string
testyisTestingStrings
11.string
11.string
11.string
  • for example "testy" and "isTestingStrings" should be on 2 separate lines.
  • each line could have any number of(0 or more) "(xxxx...)" parenthesis with content and in these cases should also retain the newlines.

Why does replacing the matched text with an empty string result in the newline character being removed from the string, and how can I resolve this issue in my script. Is there better regex to handle this or would this require writing a custom js parser?

CodePudding user response:

Have you tried the following?

inputString.replace(/\([^)]*\)/g,"")

s=`06.abbaa(bbbbadf)
01.lijer      ttt
06.foo(sample)dddddddddd(garbage)
06.text(lksdhfljkld)20001(kkkkkksssss)
01.asdfg
13.fffffffff(fdsgggg/asdfasdf)
03.ggggggg(defg/abc)
18.abc     123abc
blahblah
qqqqqq
anon
06.barbarbar
10.foobar
18.fooistbar
12.blahhhh(moreblah/blah)
15.2035number(test)(test0/2test)
12.testing morewords
03.random(blahhh)(blahahaha/morerandomstuff)
17.anotherrandomtextstring
11.string
testy(mctest)
isTestingStrings
11.string
11.string
11.string`;


console.log(s.replace(/\([^)]*\)/g,""))

CodePudding user response:

The reason why new newline characters are being removed from your string is that newline characters are considered spacing characters, and are matched by the two \s in your regular expression. If you want to keep newlines but remove whitespaces and tabs before and after parentheses, change \s to [\t ]:

updatedText = updatedText.replaceAll(/[\t ]*\(.*?\)[\t ]*/g, '');

If you never meant to remove spacing characters and you just want to remove parentheses along with the text inside then, just remove the \s* sequences from your regex:

updatedText = updatedText.replaceAll(/\(.*?\)/g, '');
  •  Tags:  
  • Related