为文本文件中的每个项添加引号

时间:2022-08-15 00:31:14

I want to use this list of bad words for my input filtering. It's a plain list right now, but I need to convert it to JSON for my server to use.

我想在输入过滤中使用这个坏词列表。它现在是一个简单的列表,但我需要将其转换为JSON以供我的服务器使用。

I don't want to go through each line and add quotes and a ,. Is there a regex or fast way to add " ", to each line in a txt file?

我不想通过每一行并添加引号和a。是否有正则表达式或快速方法添加“”到txt文件中的每一行?

Such that:

这样:

2g1c
2 girls 1 cup
acrotomophilia
alabama hot pocket
alaskan pipeline

Becomes

"2g1c",
"2 girls 1 cup",
"acrotomophilia",
"alabama hot pocket",
"alaskan pipeline",
...

4 个解决方案

#1


1  

Use backtick `

使用反引号`

var txt=`2g1c
2 girls 1 cup
acrotomophilia
alabama hot pocket
alaskan pipeline`;

var arrayUntrimmed = txt.split("\n");

var array=arrayUntrimmed.map(function(a){return a.trim()});

(Note: This ECMAScript 6 feature supported from Firefox 34 and Chrome 41)

(注意:Firefox 34和Chrome 41支持此ECMAScript 6功能)

#2


1  

All you have to do is split the string at the new lines and drop the last item in the array (since it's empty).

您所要做的就是在新行处拆分字符串并删除数组中的最后一项(因为它是空的)。

var txt = '2g1c\n2 girls 1 cup\nacrotomophilia\nalabama hot pocket\nalaskan pipeline\n';
var array = txt.split('\n').slice(0, -1);


console.log(array)

You can then use Array.prototype.some as a predicate method to find out if a given string contains one or more of the blacklisted words.

然后,您可以使用Array.prototype.some作为谓词方法来查明给定字符串是否包含一个或多个列入黑名单的单词。

var txt = '2g1c\n2 girls 1 cup\nacrotomophilia\nalabama hot pocket\nalaskan pipeline\n';
var array = txt.split('\n').slice(0, -1);

var input1 = 'not bad';
var input2 = 'An alaskan pipeline is quite creative...';

var input1HasBadWords = array.some(function (word) {
  return input1.indexOf(word) > -1;
});

var input2HasBadWords = array.some(function (word) {
  return input2.indexOf(word) > -1;
});

console.log('input1 is: ' + input1HasBadWords);
console.log('input2 is: ' + input2HasBadWords);

Your controller would look something like so:

您的控制器看起来像这样:

const fs = require('fs');

app.post('/route', (req, res) => {
  fs.readFile('/etc/hosts', 'utf8', (err, data) => {
    if (err) {
      res.sendStatus(500);
    }

    const badWords = data.split('\n').slice(0, -1);
    const hasBadWords = badWords.some((word) => {
       return req.body.input.indexOf(word) > -1;
    });

    if(hasBadWords) {
      res.send('Dirty mouth? Clean it with orbit!');
    } else {
      res.send('You are very polite');
    }
  });
});

#3


0  

http://pastebin.com/U5phzWUM

http://pastebin.com/U5phzWUM

I guess the easiest way is to use a software for this. It took me 30 sec to do this with SublimeText

我想最简单的方法就是使用软件。使用SublimeText我花了30秒

http://www.sublimetext.com/docs/selection

http://www.sublimetext.com/docs/selection

#4


0  

You can use readline module. read and add quotes in each line.

您可以使用readline模块。阅读并在每一行中添加引号。

readline: https://nodejs.org/api/readline.html

readline:https://nodejs.org/api/readline.html

#1


1  

Use backtick `

使用反引号`

var txt=`2g1c
2 girls 1 cup
acrotomophilia
alabama hot pocket
alaskan pipeline`;

var arrayUntrimmed = txt.split("\n");

var array=arrayUntrimmed.map(function(a){return a.trim()});

(Note: This ECMAScript 6 feature supported from Firefox 34 and Chrome 41)

(注意:Firefox 34和Chrome 41支持此ECMAScript 6功能)

#2


1  

All you have to do is split the string at the new lines and drop the last item in the array (since it's empty).

您所要做的就是在新行处拆分字符串并删除数组中的最后一项(因为它是空的)。

var txt = '2g1c\n2 girls 1 cup\nacrotomophilia\nalabama hot pocket\nalaskan pipeline\n';
var array = txt.split('\n').slice(0, -1);


console.log(array)

You can then use Array.prototype.some as a predicate method to find out if a given string contains one or more of the blacklisted words.

然后,您可以使用Array.prototype.some作为谓词方法来查明给定字符串是否包含一个或多个列入黑名单的单词。

var txt = '2g1c\n2 girls 1 cup\nacrotomophilia\nalabama hot pocket\nalaskan pipeline\n';
var array = txt.split('\n').slice(0, -1);

var input1 = 'not bad';
var input2 = 'An alaskan pipeline is quite creative...';

var input1HasBadWords = array.some(function (word) {
  return input1.indexOf(word) > -1;
});

var input2HasBadWords = array.some(function (word) {
  return input2.indexOf(word) > -1;
});

console.log('input1 is: ' + input1HasBadWords);
console.log('input2 is: ' + input2HasBadWords);

Your controller would look something like so:

您的控制器看起来像这样:

const fs = require('fs');

app.post('/route', (req, res) => {
  fs.readFile('/etc/hosts', 'utf8', (err, data) => {
    if (err) {
      res.sendStatus(500);
    }

    const badWords = data.split('\n').slice(0, -1);
    const hasBadWords = badWords.some((word) => {
       return req.body.input.indexOf(word) > -1;
    });

    if(hasBadWords) {
      res.send('Dirty mouth? Clean it with orbit!');
    } else {
      res.send('You are very polite');
    }
  });
});

#3


0  

http://pastebin.com/U5phzWUM

http://pastebin.com/U5phzWUM

I guess the easiest way is to use a software for this. It took me 30 sec to do this with SublimeText

我想最简单的方法就是使用软件。使用SublimeText我花了30秒

http://www.sublimetext.com/docs/selection

http://www.sublimetext.com/docs/selection

#4


0  

You can use readline module. read and add quotes in each line.

您可以使用readline模块。阅读并在每一行中添加引号。

readline: https://nodejs.org/api/readline.html

readline:https://nodejs.org/api/readline.html