I wrote this code:

var arr = ['ясно','понятно']; var a = new RegExp(arr, 'i'); var b = 'ясно'; b.match(a); 

For example, in the arr 2 array, the words: 'clear', 'clear', and these words can be searched separately. Those. separately: 'clear', separately: 'clear'.

How to set several words for search correctly and how to bring the code into working condition?

  • 2
    var a = new RegExp(arr.join('|'), 'i'); - kmv

1 answer 1

If you want to look for two words "clear" and "clear" with one regular expression, you can use the symbol | allows to implement logic in regular expressions OR

 var a = new RegExp('ясно|понятно', 'i'); 

using an array from your example, you can generate a regular expression by using the join() method

 var arr = ['ясно','понятно']; var a = new RegExp(arr.join('|'), 'i');