首页 > 解决方案 > 使用 Node.js 处理 TXT 文件中的非法字符(撇号)

问题描述

我依赖在 Node.js 中从外部发送的 .txt 文件,这些文件有时会包含我将其归类为“非法”字符的内容,例如撇号和逗号,从而导致从网页和程序(例如 Microsoft Word)复制和粘贴

如何获取 Node.js 或使用 Javascript 来替换这些不正确的格式,例如用正确格式的撇号替换撇号或去掉任何非法字符句号?

这是来自网页的示例,并显示在 PasteBin 中:

Resilience is what happens when we’re able to move forward even when things don’t fit together the way we expect.

And tolerances are an engineer’s measurement of how well the parts meet spec. (The word ‘precision’ comes to mind). A 2018 Lexus is better than 1968 Camaro because every single part in the car fits together dramatically better. The tolerances are more narrow now.

One way to ensure that things work out the way you hope is to spend the time and money to ensure that every part, every form, every worker meets spec. Tighten your spec, increase precision and you’ll discover that systems become more reliable.

The other alternative is to embrace the fact that nothing is ever exactly on spec, and to build resilient systems.

You’ll probably find that while precision feels like the way forward, resilience, the ability to thrive when things go wrong, is a much safer bet.

The trap? Hoping for one, the other or both but not doing the work to make it likely. What will you do when it doesn’t work?

Neither resilience nor tolerances get better on their own.

https://pastebin.com/uJ7GAKk4

从以下 URL 复制并粘贴到记事本中并保存

https://seths.blog/storyoftheweek/

标签: javascriptnode.jsnode-modules

解决方案


您可以使用RegExp删除不需要的字符

// text is the pasted text
var filtered = text.replace(/[',]/gm, '');

推荐阅读