0

I'm currently repeating the task like below:

var parts = "<h1>I</h1><p>am a cow;</p>cows say moo. MOOOOO."
    .split(/(\bmoo+\b)/gi);
for (var i = 1; i < parts.length; i += 2) {
  parts[i] = renderToString(<MyComponent key={i}>{parts[i]}</MyComponent>);
}

var anotherParts = parts.split(/(\bcow+\b)/gi);
for (var i = 1; i < anotherParts.length; i += 2) {
   anotherParts[i] = renderToString(<MyOtherComponent key={i}>
        {anotherParts[i]}
     </MyOtherComponent>)
}

return anotherParts

Is this possible to do the above task at once? I'm looking for most feasible way to do that because I wanted to replace more and more multiple strings.

5
  • 1
    replace() method with global option Commented Aug 19, 2018 at 9:27
  • Is var anotherParts= parts.split() giving an error? Commented Aug 19, 2018 at 9:34
  • It's returning the string.. updated the question for that. Commented Aug 19, 2018 at 9:34
  • It's unclear what is the problem and what you're trying to achieve. Please provide desirable output for this input. Should cow and moo be counted separately? Then this cannot be done at once. i += 2 - what is that for? Commented Aug 19, 2018 at 12:49
  • You should be using regex if you want to replace multiple instances at once Commented Apr 3, 2024 at 10:30

0

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.