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

时间:2018-04-12 01:47:39

标签: javascript json regex string emoji

如何使用javascript从字符串中删除所有实例:smile:style emjois?下面是我在JSON中使用的示例:point_right:in。我喜欢从字符串中删除所有这些内容。

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

4 个答案:

答案 0 :(得分:0)

只需将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);

答案 1 :(得分:0)

假设表情符号都是: 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);

答案 2 :(得分:0)

从这个答案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 :(得分:0)

试试这个:

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