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

137
Views
Is there way to change a substring in js?

I have a string like:

def definition():

I want to change word def (for example), every instance of word def but not the "def"s that are part of other words like this

console.log("def definition():".specialReplace("def", "abc"));

and result should be

abc definition():

not

abc abcinition():
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use String#replace or String#replaceAll with a regular expression:

const specialReplace = (str) => str.replaceAll(/\bdef\b/g, 'abc')
console.log(specialReplace("def definition")) // abc definition
console.log(specialReplace("def definition def")) // abc definition abc

In the regular expression, \b is a boundary type assertion that matches any word boundary, such as between a letter and a space.

Note that the same sequence \b is also used inside character class regular expression positions ([\b]), to match the backspace character.

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!