Home  >  Article  >  Web Front-end  >  Tips on using JS+regex

Tips on using JS+regex

php中世界最好的语言
php中世界最好的语言Original
2018-04-18 15:12:591302browse

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_&#39; + emojiIndex + &#39;.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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn