使用Javascript对Node.js中的文本内容执行多个Regex过滤器

问题描述 投票:1回答:3

我有多个正则表达式过滤器,我想在Node中的.txt文件上运行。我读取文件然后将内容设置为变量,然后我想用正则表达式解析内容以删除任何非法字符。

我最初尝试使用我发现的唯一一个Node模块,称为https://www.npmjs.com/package/clean-text-utils - 但它似乎是针对Typescript而我无法使用它来使用Node 8.10。所以我挖到node_module来找到相关的JS来尝试使用该函数替换非法的charcters。

如何在myTXT变量上运行所有正则表达式过滤器?目前,它只输出带有不正确的非ASCII撇号的文本。

var myTXT;

...

const readFile = util.promisify(fs.readFile);
await readFile('/tmp/' + myfile, 'utf8')
    .then((text) => {
        console.log('Output contents: ', text);
        myTXT = text;
    })
    .catch((err) => {
        console.log('Error', err);
    });

var myTXT = function (myTXT) {
    var s = text
        .replace(/[‘’\u2018\u2019\u201A]/g, '\'')
        .replace(/[“”\u201C\u201D\u201E]/g, '"')
        .replace(/\u2026/g, '...')
        .replace(/[\u2013\u2014]/g, '-');
    return s.trim();
};

console.log('ReplaceSmartChars is', myTXT);

下面是通过从网页复制文本并粘贴到.txt文件中引起的撇号问题的示例,也显示在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.

https://pastebin.com/uJ7GAKk4

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

https://seths.blog/storyoftheweek/

javascript node.js regex ascii non-ascii-characters
3个回答
2
投票

目前您没有调用执行替换的函数,而是用文本覆盖函数。

const readFile = util.promisify(fs.readFile);

function replaceChars(text) {
   return text
        .replace(/[‘’\u2018\u2019\u201A]/g, '\'')
        .replace(/[“”\u201C\u201D\u201E]/g, '"')
        .replace(/\u2026/g, '...')
        .replace(/[\u2013\u2014]/g, '-')
        .trim();
}

const myTXT = await readFile('/tmp/' + myfile, 'utf8')
    .then((text) => {
        console.log('Output contents: ', text);
        return replaceChars(text);
    })
    .catch((err) => {
        console.log('Error', err);
    });

console.log('ReplaceSmartChars is', myTXT);

0
投票

你应该把你的console放在async lambda中。并将myTXT函数重命名为与myTXT变量不同的函数。

试试下面的代码。

const fs = require('fs');

var myTXT;

(async () => {
    const readFile = util.promisify(fs.readFile);
    await readFile('/tmp/' + myfile, 'utf8')
      .then((text) => {
          console.log('Output contents: ', text);
          myTXT = text;
      })
      .catch((err) => {
          console.log('Error', err);
      });

    var replace = function (text) {
      var s = text
          .replace(/[‘’\u2018\u2019\u201A]/g, '\'')
          .replace(/[“”\u201C\u201D\u201E]/g, '"')
          .replace(/\u2026/g, '...')
          .replace(/[\u2013\u2014]/g, '-');
      return s.trim();
    };

  console.log('ReplaceSmartChars is', replace(myTXT));
})()

0
投票

我不知道clean-text-utils所以我尝试了模块,它工作得很好:

const fs = require('fs-then-native')
const cleanTextUtils = require('clean-text-utils');

async function clean(file) {
  let txt = await fs.readFile(file, 'utf8');
  txt = cleanTextUtils.replace.exoticChars(txt);
  return txt;
}

clean('input.txt')
  .then(result => {
    console.log(result);
  });
© www.soinside.com 2019 - 2024. All rights reserved.