Javascript: Text Replace for multiple strings in array?

javascript, jquery, replace

Solution

hashtags.forEach(function (elem) {
    tw.text = tw.text.replace('#' + elem, '<span class="hash">#' + elem + "</span>");
});

This does not account for tags that contain other tags which could lead to duplicate replacement.

Problem

I have an array with Twitter hashtags. And I want to filter a string `tw.text` for those hashtags and wrap the words in a `span` ``` var hashtags = new Array("home","car", "tree"); tw.text.replace('#home', '<span class="hash">#home</span>') ``` How would I do that? Thank you in advance.

Original source