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.
You need to specify the left- and right-hand side context for your pattern.
If you want to make sure there are no word chars immediately on the left and right, you need unambiguous word boundaries that are expressed with (?<!\w)
and (?!\w)
lookarounds:
new RegExp('(?<!\\w)(?:' + Object.keys(CHARS).map(escape).join("|") + ')(?!\\w)', "g")
Or, alternatively, you can rely on whitespace boundaries, that require whitespace char or start/end of string on both ends:
new RegExp('(?<!\\S)(?:' + Object.keys(CHARS).map(escape).join("|") + ')(?!\\S)', "g")
Choose the solution based on your requirements and precise the patterns in the lookarounds if you need to further precise boundary patterns.