从JavaScript字符串中删除零宽度空格字符 [英] Remove zero-width space characters from a JavaScript string

查看:197
本文介绍了从JavaScript字符串中删除零宽度空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我接受用户输入(JS代码)并实时执行(处理)它们以显示一些输出。

I take user-input (JS code) and execute (process) them in realtime to show some output.

有时代码具有零宽度空间,它是真的很奇怪。我不知道用户是如何输入的。示例 - ($。length === 3

Sometimes the code has those zero width space, it's really weird. i don't know how the users are input'ing that. Example - "(​$".length === 3

我需要能够删除该字符从我在JS中的代码。我该怎么做?或者可能还有其他一些方法来执行JS代码,以便浏览器不考虑零宽度空格字符?

I need to be able to remove that character from my code in JS. How do I do so ? or maybe theres some other way to execute that JS code so that the browser doesn't takes the zero width space characters into account ?

推荐答案

Unicode具有以下零宽度字符:

Unicode has the following zero-width characters:


  • U + 200B零宽度space

  • U + 200C零宽度非连接器Unicode代码点

  • U + 200D零宽度连接器Unicode代码点

  • U + FEFF零宽度不间断空格Unicode代码点

  • U+200B zero width space
  • U+200C zero width non-joiner Unicode code point
  • U+200D zero width joiner Unicode code point
  • U+FEFF zero width no-break space Unicode code point

要从JavaScript中的字符串中删除它们,您可以使用简单的正则表达式:

To remove them from a string in JavaScript, you can use a simple regular expression:

var userInput = 'a\u200Bb\u200Cc\u200Dd\uFEFFe';
console.log(userInput.length); // 9
var result = userInput.replace(/[\u200B-\u200D\uFEFF]/g, '');
console.log(result.length); // 5

请注意,还有更多符号可能不可见。例如, ASCII的控制字符中的一部分。

Note that there are many more symbols that may not be visible. Some of ASCII’s control characters, for example.

这篇关于从JavaScript字符串中删除零宽度空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆