此函数当前使用正则表达式从字符串中删除所有表情符号字符。如何将其反转,以便删除除表情符号字符之外的所有字符?
var ranges = [
'\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
'\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
'\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
];
function removeEmoji() {
var str = $('#emoji').html();
str = str.replace(new RegExp(ranges.join('|'), 'g'), '');
$("#emoji").html(str);
}
$('button').on('click', function() {
removeEmoji();
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="emoji">
<li></li>
<li>or Smiling Face</li>
<li>or Smiling Face</li>
<li>or Smiling Face</li>
<li></li>
<li>or Smiling Face</li>
<li>or Smiling Face</li>
<li>or Smiling Face</li>
</ul>
<button>Push Me</button>
&#13;
答案 0 :(得分:0)
只需将str.replace()
替换为str.match()
!
请注意,我已从您的答案中复制了此片段,并且正在获取并替换<ul>
的整个html。这包括<li>
元素。
var ranges = [
'\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
'\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
'\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
];
function removeEmoji() {
var str = $('#emoji').html();
str = str.match(new RegExp(ranges.join('|'), 'g'), '');
$("#emoji").html(str);
}
$('button').on('click', function() {
removeEmoji();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="emoji">
<li></li>
<li>or Smiling Face</li>
<li>or Smiling Face</li>
<li>or Smiling Face</li>
<li></li>
<li>or Smiling Face</li>
<li>or Smiling Face</li>
<li>or Smiling Face</li>
</ul>
<button>Push Me</button>