3

I need to check all elements of an array (strings) if they matches any regex, which is also stored in an array.

So here is the string-array and and regex array (in this example all three elements are the same regex - I know that doesn't make sence):

let array = [ 'just some', 'strings', 'which should be tested', 'by regex' ];
let regexes = [ /([^.]+)[.\s]*/g, /([^.]+)[.\s]*/g, /([^.]+)[.\s]*/g ];

Now I would do two _.each-loops like this:

_.each(array, function(element) {
    _.each(regexes, function(regex) {
        let match = regex.exec(element);
        if (match && match.length)
            doSomething(match);
    });
});

But what I want to achieve is, that if only one regex is matching, I want to process this string. So with this senseless regexes-array, this would never be the case, as there would be no or three matching regex.

Furthermore I would like to knwo if it possible to avoid this nested each-loop.

Update

Example:

let array = [ '1. string', 'word', '123' ]
let regexes = [/([a-z]+)/, /([0-9]+)/]

array[0] should NOT pass the test, as both regex are matching
array[1] should pass the test, as just ONE regex is matching
array[2] should pass the test, as just ONE regex is matching

so only the result for array[1] and array[2] should be used for further processing doSomething(match)

2 Answers 2

3

You could use Array#reduce and count the matches. If count equal 1, then process further.

var array = ['1. string', 'word', '123'],
    regexes = [/([a-z]+)/, /([0-9]+)/];

array.forEach(function (a) {
    var match,
        count = regexes.reduce(function (count, r) {
            var test = r.exec(a);
            if (!test) {
                return count;
            }
            match = test;
            return count + 1;
        }, 0);

    count === 1 && console.log(match);
});

Sign up to request clarification or add additional context in comments.

7 Comments

Do I need the var = match? Couldn't I just use regexes.some(r => r.exec(a))?
only if you like something to do with match.
some checks if any of the elements in an array pass a test, correct? So the result would be the same, if one regex matches or multiple regexes matches the string, right? But I want to check if only one regex matches the string. If multiple regexes are matching it should be also invalid, as no matching.
i do not understand, should all regex get used or just a single? that if only one regex is matching, I want to process this string looks like only one must match.
Sorry for my bad explanation. I want to check all strings - so far no problem. Every string should be checked for every regex. Normally there should only be one matching regex. But if there is a mistake in the input data, everything could be messed up. That's why I want to be sure there is only one matching regex. In my example - as there are three identical regexes - the result should be invalid as there are all three regexes matching. There should only be ONE matching regex for further processing. If there is NO matching or MULTIPLE matching, nothing will be processed.
|
0

You can combine Array.prototype.filter and Array.prototype.every:

let array = ['1. string', 'word', '123'],
  regexes = [/([a-z]+)/, /([0-9]+)/];


var result = array.filter(str => {
  var count = 0;
  return regexes.every(reg => {
    reg.test(str) && count++;
    return count <= 1;
  });
});

console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.