0

I made loop for, from split string and try to find some text and replace it by the result of separate string, here for example :

function replaceStr(str, find, replace) {
  for (var i = 0; i < find.length; i++) {
    str = str.replace(new RegExp(find[i], 'gi'), replace[i]);
  }
  return str;
}

var str = 'some text contain car and some house contain car, or car contain someone';
var values = "cat,dog,chicken";

splt = values.split(',');

for (i = 0; i < splt.length; i++) {
  var find = ['car'];
  var replace = ['' + values[i] + ''];
  replaced = replaceStr(str, find, replace);
}
console.log(replaced);
//console.log(splt.length);

but the result return zeros

I want find all "car" text and replace it from splited text by comma characters anyone can help me please..

5
  • ['' + values[i] + '']; - Why the ''? And why values[i]? values is a string. Should be splt[i] Commented Nov 20, 2020 at 14:19
  • Please add desired outcome. Do you want to replace each car with all the words in values? Or one at the time? Commented Nov 20, 2020 at 14:21
  • Well! above of what @Abdreas has said there is also error in your Values Array, there is no Car text in your array Commented Nov 20, 2020 at 14:22
  • The whole thing doesn’t appear to make much sense. Why are you looping in two places here? Why are you passing arrays to your replaceStr function, that only ever contain one single element? And if you only want to replace one single occurrence of the search term at a time (I’m assuming you do), then why does your regex contain the g modifier? Commented Nov 20, 2020 at 14:23
  • @CBroe i copied that function replace from this site :D.. it's always work for every string ^^ i even don't understand what I'm doing,, maybe I better explain in words instead of sample code ^^ Commented Nov 20, 2020 at 14:38

2 Answers 2

2

hum what is the goal of your replaceStr function ? maybe this is enough :

var str = 'some text contain car and some house contain car, or car contain someone';
var values = "cat,dog,chicken";

splt = values.split(',');

for (i = 0; i < splt.length; i++) {
   var find = 'car';
   str = str.replace(find, splt[i]);
}
console.log(str);
Sign up to request clarification or add additional context in comments.

Comments

1

I guess implicitly from your question you want to replace "car" with cat, dog, chicken progressively to achieve this: "some text contain cat and some house contain dog, or chicken contain someone"

So roughly this would be your solution:

var str = 'some text contain car and some house contain car, or car contain someone';
var values = "cat,dog,chicken";
var splt = values.split(',');
var replaced = str;
for (i = 0; i < splt.length; i++) {
  replaced = replaced.replace('car', splt[i]);  
}
console.log(replaced);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.