JS中关于正则的巧妙操作
程序员文章站
2022-04-29 07:49:41
废话不多说了,直接给大家贴代码了,具体代码如下所示:
var msg="dsada[emoji:37]dsadas[emoji:3900]法拉綏芬河";
fun...
废话不多说了,直接给大家贴代码了,具体代码如下所示:
var msg="dsada[emoji:37]dsadas[emoji:3900]法拉綏芬河"; function fetch(msg) { var match, result = msg, reg = /\[emoji:\d+\]/g, emojiindex, totalemojinum = 45; while (match = reg.exec(msg)) { console.log(match[0]);//[emoji:37],[emoji:3900] emojiindex = match[0].slice(7, -1); console.log(emojiindex);//37,3900 if (emojiindex > totalemojinum) { result = result.replace(match[0], '[x]'); } else { result = result.replace(match[0], '<img src="images/eomji_' + emojiindex + '.gif" />'); }; }; return result;//"dsada<img src="images/eomji_37.gif" />dsadas[x]法拉綏芬河" } fetch(msg);
总结
以上所述是小编给大家介绍的js中关于正则的巧妙操作,希望对大家有所帮助