I have the following function which replaces string codes with given emoji characters.
export const CHARS = {
":)": "",
"(:": "",
":-)": "",
":(": "",
"):": "",
":-(": "",
":/": "",
}
convertEmoji = string => {
const escape = s => s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")
const pattern = new RegExp(
Object.keys(CHARS).map(escape).join("|"),
"g"
)
return string.replace(pattern, match => CHARS[match])
}
This works as expected but it also converts when any of the given CHARS[key] is present in a word.
For example, passing this function to a string that contains a URL:
https://google.com
Becomes:
https/google.com
I want the replace function to be worked only when the emoji code is presented as words in a given sentence. For example:
:) Hello world! - Here the function should work.
:)Hello world! - Here it should not work as the emoji code does not have the correct spaces.
Please help me fix this.