S
S
svilkov872018-03-23 00:41:34
JavaScript
svilkov87, 2018-03-23 00:41:34

How to check for matches in an array with regular expressions?

Hello.
I have never encountered checking for a match of characters in an array.
For example, there is an array and a string:

var arr = ['@','.ru','.com'],
string = '[email protected]';

How to check in a string for the existence of characters corresponding to elements in an array?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-03-23
@svilkov87

var fragments = ['@','.ru','.com'];
var str = '[email protected]';
if (fragments.some(fragment => str.includes(fragment)) ) {
  // ...
}

If you need a regular expression, then/@|\.ru|\.com/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question