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

查看:72
本文介绍了从 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 spaces; it's really weird. I don't know how the users are inputting 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 there's some other way to execute that JS code so that the browser doesn't take the zero-width space characters into account?

推荐答案

Unicode 具有以下零宽度字符:

Unicode has the following zero-width characters:

  • U+200B 零宽度空间
  • U+200C 零宽度非连接器 Unicode 代码点
  • U+200D 零宽度连接器 Unicode 代码点
  • U+FEFF 零宽度不间断空格 Unicode 代码点

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

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

var userInput = 'au200Bbu200Ccu200DduFEFFe';
console.log(userInput.length); // 9
var result = userInput.replace(/[u200B-u200DuFEFF]/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天全站免登陆