search

Home  >  Q&A  >  body text

Regular expression - javascript replace emoticon characters

The code is as follows, the purpose is to replace the emoticon string with a picture

EmojiParser.parse('Hahahahaha (normal) Hahaha (surprise)');

In this way, only the first one (normal) can be replaced, and the second one cannot be replaced. How to modify it?

export default class EmojiParser {
  static emojiSeries =[{
    id: 101,
    name: 'normal'
  }, {
    id: 102,
    name: 'surprise'
  }];

  static parse(content) {
    return content.replace(/(\(.*\))/, (match) => {
      let replaceStr = '';
      for (const series of EmojiParser.emojiSeries) {
        if (match === `(${series.name})`) {
          replaceStr += `<img width="28" height="28" src="https://source.pixiv.net/common/images/emoji/${series.id}.png" />`;
        }
      }
      return replaceStr === '' ? match : replaceStr
    });
  }
}
PHP中文网PHP中文网2727 days ago3474

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-07-05 10:58:56

    /((.*))/ can be replaced by /((.*))/g. g stands for global global replacement.

    reply
    0
  • Cancelreply