Regular expression - javascript replace emoticon characters
PHP中文网
PHP中文网 2017-07-05 10:56:56
0
1
3472

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中文网

认证高级PHP讲师

reply all(1)
Ty80

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template