I
I
Ilya Pavlov2018-08-27 10:28:55
JavaScript
Ilya Pavlov, 2018-08-27 10:28:55

Reg.test() - how to check if certain words are required?

How to use a regular expression (one) to check if certain words are present in a string? That is, replace the following:

if(
  /слово1/.test(string) &&
  /слово2/.test(string) &&
  /слово3/.test(string)
)
  done()

For one regular expression, and, accordingly, one .test() check?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick Sdk, 2018-08-27
@PiCoderman

let words = ['набор', 'определенных', 'слов'];
let Reg = new RegExp(words.map(word => `.*(?=${word})`).join(''), 'i');
let text1 = 'какойто текст и набор определенных слов';
let text2 = 'какойто текст определенных слов';
Reg.test(text1);
// true
Reg.test(text2);
// false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question