Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

92
Views
How to remove the last block of text between parentheses in a string, only if there are two blocks of text between parentheses at the end of string?

I need to remove the last block of text between parentheses in a string, but only when there are two blocks of text between parentheses at the end of that string.

For instance, if the string is

Text (aaa) Text (bbb) Text (ccc) (ddd).

I need to get

Text (aaa) Text (bbb) Text (ccc).

While, if the string is

Text (aaa) Text (bbb) Text (ccc).

I need to preserve it as it is.

I've tried several regular expressions using https://regexr.com (e.g. /\s\(([^)]+)\)\s\(([^)]+)\)/), but none of them is right for me (I'm not very experienced with regex).

Would you give me any suggestions?

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use

text = text.replace(/(\([^()]*\))\s*\([^()]*\)(?![\s\S]*\([^()]*\))/, '$1')
// Or, if there is always a dot and end of string (with possible trailing whitespace)
text = text.replace(/(\([^()]*\))\s*\([^()]*\)(?=\.\s*$)/, '$1')

See the regex demo. Details:

  • (\([^()]*\)) - Group 1 ($1): (, zero or more chars other than ( and ) and then a ) char
  • \s* - zero or more whitespaces
  • \([^()]*\) - (, zero or more chars other than ( and ) and then a ) char
  • (?![\s\S]*\([^()]*\)) - a negative lookahead that ensures there is no other (...) substring anywhere to the right of the current location.
  • (?=\.\s*$) - a positive lookahead that requires ., then zero or more whitespaces and then an end of string immediately to the right of the current location.

See the JavaScript demo:

const texts = ['Text (aaa) Text (bbb) Text (ccc) (ddd).','Text (aaa) Text (bbb) Text (ccc).'];
const rx = /(\([^()]*\))\s*\([^()]*\)(?![\s\S]*\([^()]*\))/;
for (const text of texts) {
    console.log(text, '=>', text.replace(rx, '$1'));
}

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.