Home >Web Front-end >JS Tutorial >Tips on using JS+regex
This time I will bring you JS tips for using regular expressions, and what are the precautions for using JS regular expressions. The following is a practical case, let’s take a look.
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);I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
JS canvas operation gif animation
js prohibits browser back event
The above is the detailed content of Tips on using JS+regex. For more information, please follow other related articles on the PHP Chinese website!