Don_B
Don_B

Reputation: 243

Using array elements to do a search and replace all using javascript?

Say, I have a string

'I am abc1, my age is abc2 and I live in abc3, abc1'

and array which looks like

['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3']

I'm trying to do a search and replace all on the string using each element of the array in below fashion

replace abc1 by xyz1, abc2 by xyz2 etc..

i.e. after the script runs the output string will be

I am xyz1, my age is xyz2 and I live in xyz3, xyz1

This is what I tried thus far

var myString = 'I am abc1, my age is abc2 and I live in abc3, abc1';
var myArray = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3'];
for (var i=0; i<=myArray.length; i++){
var a1 = myArray[i];
var xs = a1.split("/");
var new1=xs[0];
var new2=xs[1];
var replaced = myString.replace(/new1/g, new2);
}
document.write(replaced);

But it is not working. Can anybody help?

Upvotes: 1

Views: 283

Answers (4)

PeterMader
PeterMader

Reputation: 7285

A functional approach:

var text = 'I am abc1, my age is abc2 and I live in abc3, abc1';
var words = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3'];

var result = words.reduce((text, word) => {
  var [ oldWord, newWord ] = word.split('/');
  return text.replace(new RegExp(oldWord, 'g'), newWord);
}, text);

console.log(result);

Upvotes: 0

Yosvel Quintero
Yosvel Quintero

Reputation: 19080

You can iterate over the words array usign String.prototype.forEach() and in every loop split the element w with String.prototype.split() to create an array variable a with the two indexed elements that you need to create a regular expression and call String.prototype.replace():

var text = 'I am abc1, my age is abc2 and I live in abc3, abc1';
var words = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3'];

words.forEach(function (w) {
  var a = w.split('/');
  text = text.replace(new RegExp(a[0], 'g'), a[1]);
});

console.log(text);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386736

Basically you loop one too much, take i < myArray.length and you need the value of the variable as regular expression.

You could use a constructor and build a new regular expression object.

At last, you need to replace the same string and assign to the same string, otherwise you hae replaced, but you get only the last replacement back.

var myString = 'I am abc1, my age is abc2 and I live in abc3, abc1',
    myArray = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3'],
    i, xs, new1, new2;

for (i = 0; i < myArray.length; i++) {
    xs = myArray[i].split("/");
    new1 = xs[0];
    new2 = xs[1];
    myString = myString.replace(new RegExp(new1, 'g'), new2);
}

console.log(myString);

Upvotes: 2

Suren Srapyan
Suren Srapyan

Reputation: 68665

Try this approach.

For first a split the keys/values and get a new array (keyValue) which holds the old and new words. Then I iterate over the keyValue and replace the text by it's values.

var text = 'I am abc1, my age is abc2 and I live in abc3, abc1';
var words = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3'];

var keyValue = words.map(item => {
  var arr = item.split('/');
  return [arr[0], arr[1]];
});

keyValue.forEach(item => {
  text = text.replace(new RegExp(item[0], 'g'), item[1]);
});

console.log(text);

Upvotes: 1

Related Questions