简体   繁体   English

如何使用JavaScript从文本中删除基于冒号的表情符号

[英]How to remove colon based emojis from text using javascript

How can i remove all instances of :smile: style emjois from a string using javascript? 如何使用JavaScript从字符串中删除:smile:style emjois的所有实例? Here is an example below I got in JSON with :point_right: in it. 这是下面的示例,我在JSON中添加了:point_right:。 I'd love to remove all of them from a string. 我很想从字符串中删除所有的字符串。

[ { 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' }

Just use String.prototype.replace() with a regular expression: 只需将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); 

Assuming the emojis are all one word, between : s: 假设表情符号全是一个单词,介于: 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); 

From this answer Replacing values in JSON object you could do this : 从这个答案替换JSON对象中的值,您可以执行以下操作:

 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); 

Try this : 尝试这个 :

 // 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM