JS中关于正则的巧妙操作
废话不多说了,直接给大家贴代码了,具体代码如下所示:
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);
总结
相关推荐
ailxxiaoli 2020-11-16
austindev 2020-06-28
唐文 2020-06-28
fujuan000 2020-04-30
Andrea0 2020-01-14
Trustport 2019-12-01
coinone 2019-10-29
boox 2018-10-25
TinyDolphin 2019-10-30
happinessaflower 2019-10-26
xuefeng0 2015-04-13
haixianTV 2015-08-30
yzhj00 2017-11-23
AxDoctor 2016-04-12
quanhaoH 2012-04-21
85206537 2015-09-19
88286530 2015-09-19
81246831 2015-08-24