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

88
Views
Need to replace a character in a string with two new values

var Str = "_Hello_";
var newStr = Str.replaceAll("_", "<em>");
console.log(newStr);

it out puts <em>Hello<em> I would like it to output <em>Hello</em> but I have no idea how to get the </em> on the outer "_", if anyone could help I would really appreciate it. New to coding and finding this particularly difficult.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Replace two underscores in one go, using a regular expression for the first argument passed to replaceAll:

var Str = "_Hello_";
var newStr = Str.replaceAll(/_([^_]+)_/g, "<em>$1</em>");
console.log(newStr);

NB: it is more common practice to reserve PascalCase for class/constructor names, and use camelCase for other variables. So better str =, ...etc.

about 4 years ago · Juan Pablo Isaza Report

0

I would phrase this as a regex replacement using a capture group:

var str = "_Hello_ World!";
var newStr = str.replace(/_(.*?)_/g, "<em>$1</em>");
console.log(newStr);

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!