如何从字符串中删除控制字符?

时间:2020-12-26 19:36:53

I have form on my page where user can type some text and submit it. Text is then sent to server (REST API on top of node.js) and saved to DB (postgres).

我的页面上有表单,用户可以输入一些文本并提交它。然后将文本发送到服务器(node.js之上的REST API)并保存到DB (postgres)。

The problem is that some strange characters (control characters) are saved to DB occasionaly - for example escape control character (^[) or backspace control character (^H). Generally it does not break anything since those characters are invisible, so html is rendered correctly. However when I provide xml content for RSS readers, they (readers) return "Malformed XML" because of those control characters (it works after deleting them).

问题是一些奇怪的字符(控制字符)保存到数据库有时会——例如逃避控制字符(^)或退格控制字符(^ H)。通常它不会破坏任何东西,因为这些字符是不可见的,所以html被正确地呈现。但是,当我为RSS阅读器提供xml内容时,它们(读者)会返回“格式错误的xml”,因为这些控制字符(在删除它们之后会起作用)。

My question is how I can remove those characters from a string on client level (javascript) or server level (javascript/node.js)?

我的问题是如何从客户机级(javascript)或服务器级(javascript/node.js)的字符串中删除这些字符?


I have found right answer here: removing control characters in utf-8 string

我在这里找到了正确的答案:删除utf-8字符串中的控制字符

s.replace(/[\x00-\x1F\x7F-\x9F]/g, "");

1 个解决方案

#1


0  

I had the similar problem, here's the solution which i choose.

我也遇到过类似的问题,这是我选择的解决方案。

I encoded the string data from the user using encodeURIComponent(variable_Name) and then saved then while displaying i decoded using decodeURIComponent(variable_Name)

我使用encodeURIComponent(variable_Name)对用户的字符串数据进行编码,然后在显示我使用decodeURIComponent(variable_Name)进行解码时保存

#1


0  

I had the similar problem, here's the solution which i choose.

我也遇到过类似的问题,这是我选择的解决方案。

I encoded the string data from the user using encodeURIComponent(variable_Name) and then saved then while displaying i decoded using decodeURIComponent(variable_Name)

我使用encodeURIComponent(variable_Name)对用户的字符串数据进行编码,然后在显示我使用decodeURIComponent(variable_Name)进行解码时保存