Split and Replace in JavaScript

Problem Statement:

In this problem of Split and Replace in JavaScript, we are given 3 strings namely, inputString, replaceString, and a separator. We need to replace all words in the inputString with the replaceString where the length of the word is greater than 7.

Code to Split and Replace in JavaScript:

const inputString = "I love Python";
const replaceString = "CopyAssignment"
const separator = " "
const myArray = inputString.split(separator);
for (let i = 0; i < myArray.length; i++) {
  if(myArray[i].length>7){
    myArray[i] = replaceString
  }
}
const str = myArray.join(' ');
console.log(str);

Output:

Output for Split and Replace in JavaScript

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@violet-cat-415996.hostingersite.com Thank you