// Official Twitch robot emotes: https://twitchemotes.com
var emoticons = {
':)' : 'ebf60cd72f7aa600',
':(' : 'd570c4b3b8d8fc4d',
':o' : 'ae4e17f5b9624e2f',
':z' : 'b9cbb6884788aa62',
'B)' : '2cde79cfe74c6169',
':\\' : '374120835234cb29',
';)' : '3407bf911ad2fd4a',
';p' : '3407bf911ad2fd4a',
':p' : 'e838e5e34d9f240c',
'R)' : '0536d670860bf733',
'o_O' : '8e128fa8dc1de29c',
':D' : '9f2ac5d4b53913d7',
'>(' : 'd31223e81104544a',
'<3' : '577ade91d46d7edc'
}
// Convert the emoticon map entries into their fully-qualified paths.
mapIdsToPaths(emoticons,
'https://static-cdn.jtvnw.net/jtv_user_pictures/',
'chansub-global-emoticon-',
'24x18');
// Replace all possible emotes in each paragraph.
document.querySelectorAll('p').forEach(e => e.innerHTML = replaceEmoticons(e.innerHTML, emoticons));
function replaceEmoticons(text, emotes) {
return Object.keys(emotes).reduce((result, emote) => {
return result.replace(new RegExp(RegExpEscape(emote), 'gi'), function(match) {
return (img => img != null ? '<img src="' + img + '"/>' : match)(emotes[match]);
});
}, text);
}
// helper function to escape special characters in regex
function RegExpEscape(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
// Map emote ids to their URLs.
function mapIdsToPaths(emotes, url, prefix, size) {
Object.keys(emotes).forEach((id) => {
emotes[id] = url + prefix + emotes[id] + '-' + size + '.png';
});
}
<p>Hello ;)<p>
<p>How are you :)?<p>
<p>o_O Today was not a good day... :(<p>
爲什麼你需要在firstPlace一個正則表達式?只要做'yourString.replaceAll(「:wink:」,winkSource)' –
對不起,我編輯我的帖子,我需要更換所有數組的例子';)'通過wink.png或':)'由smile.png – Miky
@Bálint我'非常確定沒有「replaceAll」字符串的原型... – ndugger