如何可靠地删除破坏代码的隐形字符? [英] How to reliably strip invisible characters that break code?

查看:133
本文介绍了如何可靠地删除破坏代码的隐形字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个bookmarklet并且遇到了这个我刚才能解决的问题:一个 \\\舃 字符,Chrome无用地告诉我我的代码块(在粘贴到JS控制台时)是一个无效的字符ILLEGAL。

I am trying to build a bookmarklet and got slammed with this issue which I was just able to figure out: a \u8203 character, which Chrome unhelpfully tells me in my block of code (upon pasting into the JS console) is an `"Invalid character ILLEGAL".

幸运的是,Safari告诉我这是 \ u8203

Luckily Safari was the one that told me it was a \u8203.

我正在编辑Sublime Text 2编辑器中的代码,并以某种方式复制进出它(我也试过TextEdit)无法删除它。

I am editing the code in the Sublime Text 2 editor and somehow copying in and out of it (I also tried TextEdit) fails to remove it.

是否有某种网站会删除除ASCII以外的所有字符?

Is there some sort of website somewhere that will strip all characters other than ASCII?

当我尝试保存为ISO 8859但由于字符不支持而将其保存为UTF-8。

When I try to save as ISO 8859 but it will save it back as UTF-8 "because of unsupported characters".

...是的。这才是重点。摆脱我不受支持的邪恶角色。

... Yeah. that's the point. Get rid of my unsupported evil characters.

我该怎么办?在十六进制编辑器中编辑我的文件?

What am I supposed to do? Edit my file in a hex editor?

FYI我实际上是通过重新输入代码来解决的(顺便说一下,这个代码来自这个网站)。

FYI I actually solved it by re-typing the code (which originated from this site by the way).

推荐答案

嗯,我能想到的最简单方法是使用 sed

Well, the easiest way I can think of is to use sed

sed -i 's/[^[:print:]]//g' your_script.js
//            ^^^^^ this can also be 'ascii'

或使用 tr

tr -cd '\11\12\15\40-\176' < old_script.js > new_script.js

这篇关于如何可靠地删除破坏代码的隐形字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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