Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Regex is unable to find and replace a string containing parentheses

I am attempting to find a sentence in a paragraph (contained in a textarea), and highlight it. All works well if the sentence does NOT contain opening and closing parenthesis, such as the example below. The replace method is unable to replace the string with its equivalent surrounded by the "mark" tags. Can anyone advise how I can update my Regex to find and replace a string containing parentheses? Do I need to find and replace all parentheses with "/(" and "/)" first?

const text = "An economy is composed of suppliers (workers) and demanders (firms)."

const regex = new RegExp(text, "\d");
// regex returns: /The market for labor, then, is composed of suppliers (workers) and demanders (firms). /
        console.log("REGEX:", regex);
        let match = text.replace(regex, "<mark>$&</mark>");
        console.log("MATCH:", match);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Highlight Text in Parenthesis

html:

<html>
  <body>
   <p id="txt">"(An economy is composed of suppliers (workers) and demanders (firms).)</p>
  </body>
</html>

JS:

function hilitetext() {
  const s = document.getElementById("txt").textContent;
  const regex = /(?:^[^(]?[^(]+)?(\(\w+\))/g;//retains parenthesis
  //const regex = /(?:^[^(]?[^(]+)?\((\w+)\)/g;//removes parenthesis
  document.getElementById("txt").textContent = s.replace(regex, "<mark>$1</mark>");
}
about 4 years 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 vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!