join your array and create a single regex, then you can use replace with a callback function (or use the single line replace like in sln's answer) to create the spans
Note this assumes all emoji names are between :
var emojies = ["smile", "smiley", "grin", "joy"];
//Create a single regex
var regex = new RegExp(":("+ emojies.join("|") +"):","g");
/* Will result in /:(smile|smiley|grin|joy):/g
* which basically is find smile OR simely OR grin OR joy
* between : and :
*/
var ele = document.getElementById("#whateverElement");
var result = ele.innerText.replace(regex,function(fullmatch,match){
//fullmatch will be like :smile:
//while match will be like smile, so you would want to use match in the class
//Using a simple jquery call to create the span
//you could also just use string concatenation
var span = jQuery("<span>",{ class: "emoji "+match });
//return the text you want to replace fullmatch with
return span[0].outerHTML;
});
ele.innerHTML = result;
Demo
var emojies = ["smile", "smiley", "grin", "joy"];
var regex = new RegExp(":("+ emojies.join("|") +"):","g");
var ele = document.getElementById("someDiv");
var result = ele.innerText.replace(regex,function(fullmatch,match){
var span = jQuery("<span>",{ class: "emoji "+match });
return span[0].outerHTML;
});
ele.innerHTML = result;
.emoji {
display:inline-block;
height:32px;
width:32px;
background-size:cover !important;
}
.emoji.joy {
background: url(http://pix.iemoji.com/images/emoji/apple/8.3/256/face-with-tears-of-joy.png) no-repeat 0 0;
}
.emoji.grin {
background: url(http://pix.iemoji.com/images/emoji/apple/8.3/256/grinning-face.png) no-repeat 0 0;
}
.emoji.smile {
background: url(http://pix.iemoji.com/images/emoji/apple/8.3/256/smiling-face-with-open-mouth-and-smiling-eyes.png) no-repeat 0 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="someDiv">
This is an example :smile:, of some text :grin: :joy: :joy:
</div>
if you want it in a single function just put the replace call in the function and return the result
var emojies = ["smile", "smiley", "grin", "joy"];
var emojiRegex = new RegExp(":("+ emojies.join("|") +"):","g");
function stringToEmoji(text){
//stealing sln's answer to make this shorter
return text.replace(emojiRegex, '<span class="emoji $1"></span>');
}
as a jQuery function
(function($){
var emojies = ["smile", "smiley", "grin", "joy"];
var emojiRegex = new RegExp(":("+ emojies.join("|") +"):","g");
$.fn.toEmoji = function(){
return this.each(function() {
this.innerHTML = this.innerText.replace(emojiRegex,function(fullmatch,match){
return '<span class="emoji '+match.toLowerCase()+'"></span>';
});
});
};
})(jQuery);
//And use like
$(document).ready(function(){
$(".content div").toEmoji();
});
JQuery function demo
(function($){
var emojies = ["smile", "smiley", "grin", "joy"];
var emojiRegex = new RegExp(":("+ emojies.join("|") +"):","g");
$.fn.toEmoji = function(){
return this.each(function() {
this.innerHTML = this.innerText.replace(emojiRegex,function(fullmatch,match){
return '<span class="emoji '+match.toLowerCase()+'"></span>';
});
});
};
})(jQuery);
//And use like
$(".content div.addemoji").toEmoji();
.emoji {
display:inline-block;
height:32px;
width:32px;
background-size:cover !important;
}
.emoji.joy {
background: url(http://pix.iemoji.com/images/emoji/apple/8.3/256/face-with-tears-of-joy.png) no-repeat 0 0;
}
.emoji.grin {
background: url(http://pix.iemoji.com/images/emoji/apple/8.3/256/grinning-face.png) no-repeat 0 0;
}
.emoji.smile {
background: url(http://pix.iemoji.com/images/emoji/apple/8.3/256/smiling-face-with-open-mouth-and-smiling-eyes.png) no-repeat 0 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="content">
<div class="addemoji">Some :grin: text :joy:</div>
Content text
<div class="addemoji">More :smile: text :joy:</div>
even more content text
<div class="addemoji">Yet some other :joy: text :grin:</div>
</div>