Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

instances of "you", "youuu", or "u" (not case sensitive) with "your client"

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

Example

String Value: "We have sent the deliverables to you."

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want result like this : "We have sent the deliverables to your client."**

String Value: "Our team is excited to finish this with you."

I want result like this : "Our team is excited to finish this with your client."

String Value: "youtube"

I want result like this : "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.’

>Solution :

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
  • `
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading