K
K
Kovalsky2016-11-09 12:54:53
JavaScript
Kovalsky, 2016-11-09 12:54:53

How to find all occurrences of strings in other strings with a regular expression?

Maybe I explained it poorly, but it means, for example, to find two-digit numbers in brackets:

101 20 (3 42 56 ) 71 1 6570 ( 40 146 16 )

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
Optimus, 2016-11-09
@lazalu68

I would do it in 2 steps. First, I took everything in brackets, and then in the found I found values ​​​​of 2 digits

S
Snewer, 2016-11-09
@Snewer

~\([^\)](\d\d\)[^\)])~

A
Alexander, 2016-11-09
@xpert13

No, regular expressions do not support nesting levels. If you really want to solve this problem with regular expressions, then you need to break the solution into stages:
1. The first regular expression searches for all brackets (and their contents)
2. The second searches for two-digit numbers already among what the first one found

B
Bowen, 2016-11-09
@Bowen

Try like this:

var txt = '101 20 (3 42 56) 71 1 6570 ( 40 146 16)';
var ar = txt.match(/\(([^()]+)\)/g);
ar.forEach(function(item) {
  var a = item.replace(/\(|\)/g, "").split(" ");
  a.forEach(function(item) {
    if (item.length === 2) {
      console.log(item);
    }
  });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question