Home > Blockchain >  instances of "you", "youuu", or "u" (not case sensitive) with "yo
instances of "you", "youuu", or "u" (not case sensitive) with "yo

Time:12-11

How to replace words places all instances of "you", "youuu", or "u" (not case sensitive) with "your client" (always lowercase).

Example

Input: "We have sent the deliverables to you."
Expected: "We have sent the deliverables to your client."

Input: "Our team is excited to finish this with you."
Expected: "Our team is excited to finish this with your client."

Input: "youtube"
Expected: "youtube"

String.prototype.replaceAll = function (target, payload) {
    let regex = new RegExp(target, 'g')
    return this.valueOf().replace(regex, payload)
};

const autocorrect = str => {
   var replace = 'your client'
//  const correction = {
//    "you": replace ,
//   "youuuu": replace,
//   "u": replace,

// };
//   Object.keys(correction).forEach((key) => {
//   str = str.replaceAll(key, correction[key]);
// });

// // var str = wordInString(text, ['you', 'youuuu','u'], replace); 
//   return str;
  
  var mapObj = {
      "you": replace ,
  "youuuu": replace,
  "u": replace,
};
   return replaceAll(str,mapObj)
};

Error expected 'Oyour clientr team is excited to finish this with your client.' to equal 'Our team is excited to finish this with your client.'

CodePudding user response:

This will do it:

[ 'We have sent the deliverables to you.',
  'Our team is excited to finish this with youuu.',
  'I like youtube'
].forEach(str => {
  let result = str.replace(/\b(?:you|youuu|u)\b/gi, 'your client');
  console.log(str   ' =>\n'   result);
});

Output:

We have sent the deliverables to you. =>
We have sent the deliverables to your client.
Our team is excited to finish this with youuu. =>
Our team is excited to finish this with your client.
I like youtube =>
I like youtube

Explanation of regex:

  • \b -- word boundary
  • (?: -- non cature group start
  • you -- literal text
  • | -- logical OR
  • youuu -- literal text
  • | -- logical OR
  • u -- literal text
  • ) -- non cature group end
  • \b -- word boundary
  • /gi -- flags for gloabl (match multiple times), and ignore case
  • `
  • Related