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

173
Views
JavaScript: match a word in a text with a word in array and replace it

I need to change this text:

var text = `this is an example text. 1coffee , 2 coffee , 1 apple, 2apple , ?banana ,carrot`;

using these 2 arrays :

var arrOld = ["coffee", "apple", "banana" , "carrot"];
var arrnew = ["laptop", "keyboard", "mouse", "printer"];

to get result like this:

`this is an example text. 1laptop , 2 laptop , 1 keyboard, 2keyboard , ?mouse ,printer`

I was trying something like:

for (let i = 0; i < arrOld.length; i++) {
arrNew[i];
arrOld[i];
text.replace(arrOld[i],arrNew[i])
}

but it didn't work.

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

0

From the docs on .replace (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace):
"The replace() method returns a new string with some or all matches of a pattern replaced by a replacement."
This means that text.replace() doesn't manipulate the text variable. To fix this, simply do the following in your loop:
text = text.replace()
This will catch the changed text into the text variable. Without doing this, you are making the changes, but you are not using them (you forget them).

about 4 years ago · Juan Pablo Isaza Report

0

You could use String.prototype.replaceAll():

var text = "this is an example text. 1coffee , 2 coffee , 1 apple, 2apple , ?banana ,carrot";
var arrOld = ["coffee", "apple", "banana", "carrot"];
var arrnew = ["laptop", "keyboard", "mouse", "printer"]
for (let i = 0; i < arrOld.length; i++) {
    text = text.replaceAll(arrOld[i], arrnew[i]);
}
console.log(text);

And maybe also Array.prototype.map():

var text = "this is an example text. 1coffee , 2 coffee , 1 apple, 2apple , ?banana ,carrot";
var arrOld = ["coffee", "apple", "banana", "carrot"];
var arrnew = ["laptop", "keyboard", "mouse", "printer"];
arrOld.map((old, i) => text = text.replaceAll(old, arrnew[i]));
console.log(text);

about 4 years ago · Juan Pablo Isaza Report

0

replace returns a new string instead of changing the old one

you can use reduce

also add RegExp because replace works only for the first match it found

const text = `this is an example text. 1coffee , 2 coffee , 1 apple, 2apple , ?banana, another banana ,carrot`;

var arrOld = ["coffee", "apple", "banana" , "carrot"];
var arrnew = ["laptop", "keyboard", "mouse", "printer"];

const resultWitoutRegexp =  arrOld.reduce((res, textToChange, i) => res.replace(textToChange, arrnew[i]) , text)

const result = arrOld.reduce((res, textToChange, i) => res.replace(new RegExp(textToChange, 'g'), arrnew[i]) , text)
console.log(resultWitoutRegexp)
console.log(result)

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!