I have an array named Sport with almost 20 items and I need to replace an image HTML string with global replacement (/string/g), my string contains HTML tags. This is an example:
//Sports array
var sport = ['soccer','tennis','pool','basketball'];
//Original string
var original = 'Hello, do you play <img width="20" src="img/soccer.png"/> ?';
//New string
var newstring;
//If original string contains an image html of some sports from array,replace with :sport[item]:
for (var i = 0; i < sport.Length; i++)
{
newstring = original.replace('/<img width="20" src="img\/icons\/'+sport[i]+'.png"/>/g',':'+sport[i]+':');
}
So, recap... I need to replace this
<img width="20" src="img/soccer.png"/>
To this
:soccer:
The result should be: Hello, do you play :soccer:?