R
R
rodgi2020-02-23 13:37:23
JavaScript
rodgi, 2020-02-23 13:37:23

How to check the presence of a word in a txt file?

I create a condition for the presence of prohibited words (in my case, links).
I already read the file with fs, but I don't know how to use this list.
Tell me please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
CatCatcher, 2020-02-23
@rodgi

First, make an array of words from the read file, unless, of course, you have already done so.
Then, when a new message is received, through for go through all the words

//badWords - переменная, в которую, предположим, и записан этот массив
for (let word of badWords) {
    if (message.content.includes(word)) {}
};

H
hzzzzl, 2020-02-23
@hzzzzl

// запрещенные слова
array = ["ыва", "файл", "уй", "иг_Л"]

// регулярное выражение
blacklist = new RegExp(array.join("|"), 'gi')

text = /* содержимое прочитанного файла */

blacklist.test(text)
// true если найдено, false если нет

UPD will need another regular expression that will escape unsuitable special characters in array

K
Karpion, 2020-02-23
@Karpion

It is necessary to cram all the text into one string variable and apply regex to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question