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."
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 startyou— literal text|— logical ORyouuu— literal text|— logical ORu— literal text)— non cature group end\b— word boundary/gi— flags for gloabl (match multiple times), and ignore case- `