【问题标题】:How to remove colon based emojis from text using javascript如何使用 javascript 从文本中删除基于冒号的表情符号
【发布时间】:2018-09-22 00:40:18
【问题描述】:

如何使用 javascript 从字符串中删除 :smile: style emjois 的所有实例?下面是我在 JSON 中获得的一个示例,其中包含 :point_right:。我很想从一个字符串中删除所有这些。

[ { service_name: 'Instagram',
   title: 'Instagram: “:point_right: Real people, making real products from real plants, using their actual hands to put them in boxes that show up on your doorstep.…”',
   text: '36 Likes, 2 Comments - “:point_right: Real people, making real products',
  ts: '1523497358.000299' }

【问题讨论】:

    标签: javascript json regex string emoji


    【解决方案1】:

    试试这个:

    // JSON Object
    var jsonObj = [{
    	"service_name": "Instagram",
    	"title": "Instagram: “:point_right: Real people, making real products from real plants, using their actual hands to put them in boxes that show up on your doorstep.…”",
    	"text": "36 Likes, 2 Comments - “:point_right: Real people, making real products",
    	"ts": "1523497358.000299"
    }];
    
    // Replace :point_right: with blank string.
    jsonObj.map(obj => {
      obj.title = obj.title.replace(":point_right: ", "");
      obj.text = obj.text.replace(":point_right: ", "");
      return obj;
    });
    
    // Output
    console.log(jsonObj);

    【讨论】:

      【解决方案2】:

      从这个答案Replacing values in JSON object 你可以这样做:

      var json=[ { service_name: 'Instagram',
         title: 'Instagram: “:point_right: Real people, making real products from real plants, using their actual hands to put them in boxes that show up on your doorstep.…”',
         text: '36 Likes, 2 Comments - “:point_right: Real people, making real products',
        ts: '1523497358.000299' }];
      
      var rep = JSON.stringify(json).replace(/(“)(:[^:]+:)/g, '$1'); 
      var New = JSON.parse(rep); 
      console.log(New);

      【讨论】:

        【解决方案3】:

        假设表情符号都是一个单词,在:s 之间:

        const obj = {
          service_name: 'Instagram',
          title: 'Instagram: “:point_right: Real people, making real products from real plants, using their actual hands to put them in boxes that show up on your doorstep.…”',
          text: '36 Likes, 2 Comments - “:point_right: Real people, making real products',
          ts: '1523497358.000299'
        }
        
        obj.title = obj.title.replace(/:[^ ]+:/g, '');
        obj.text = obj.text.replace(/:[^ ]+:/g, '');
        console.log(obj);

        【讨论】:

          【解决方案4】:

          只需将String.prototype.replace() 与正则表达式一起使用:

          const input = 'Instagram: “:point_right: Real people, making real products from real plants, using their actual hands to put them in boxes that show up on your doorstep.…”';
          
          const output = input.replace(/:\w+:/g, '');
          
          console.log(output);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-01-01
            • 2017-10-09
            • 2018-12-15
            • 2012-06-15
            • 1970-01-01
            • 2017-08-23
            • 2016-10-02
            • 2018-04-05
            相关资源
            最近更新 更多