JavaScript regex get all numbers but exclude all between brackets

I have string:

123 df456 555 [ 789 ] [abc 1011 def ] [ ghi 1213] [jkl mno 1415 pqr] 161718 jkl 1920

  • Understanding the Context of Curly Brackets '{}'
  • get all inner brackets/parentheses
  • use brackets in checkbox name when using php and javascript?
  • How does the ~[] construction work in JavaScript?
  • Fix Sublime Text 2 brackets matching with > 28 if()-tests?
  • JavaScript: comma after opening bracket of parameter in syntax example
  • I need to get only numbers, that don’t placed between square brackets [ ].
    All the resulted numbers I need to place inside square brackets [ ]
    The correct result should be:

    [123] df456 [555] [ 789 ] [abc 1011 def ] [ ghi 1213] [jkl mno 1415 pqr] [161718] jkl [1920]

    I’ve tried to write such JavaScript regex:
    /(?!\[(.*?)\])((\s|^)(\d+?)(\s|$))/ig

    but it seems to be wrong, it seems like positive lookahead has more priority than negative lookahead.

  • difference between dot notation and bracket notation in javascript
  • What is this JavaScript construct: “ = [y for each …]”
  • Curly Brackets in Arrow Functions
  • use brackets in checkbox name when using php and javascript?
  • How does the ~[] construction work in JavaScript?
  • Understanding the Context of Curly Brackets '{}'
  • 4 Solutions collect form web for “JavaScript regex get all numbers but exclude all between brackets”

    Assuming square brackets are balanced and un-nested, you can also use a negative lookahead to grab numbers outside [...]:

    var str = '1232 [dfgdfgsdf 45] 1234 [ blabla 101112 ] 67890 [113141516 ] bla171819 212123';
    var re = /\b\d+\b(?![^[]*\])/g;
    
    var repl = str.replace(re, "[$&]");
    
    console.log(repl);
    //=> [1232] [dfgdfgsdf 45] [1234] [ blabla 101112 ] [67890] [113141516 ] bla171819 [212123]

    Match all substrings between [ and ] and match and capture those others that are whole words (inside word boundaries):

    /\[[^\][]*\]|\b(\d+)\b/g
    

    See the regex demo and a demo code below.

    Details:

    • \[[^\][]*\][, then 0+ chars other than [ and ], and a ]
    • | – or
    • \b – leading word boundary
    • (\d+) – Group 1 capturing one or more digits
    • \b – trailing word boundary
    • /g – global, multiple occurrences are expected
    var regex = /\[[^\][]*\]|\b(\d+)\b/ig;
    var str = '1234 [ blabla 101112 ] 67890 [113141516 ] bla171819 212123';
    var res = [];
    while ((m = regex.exec(str)) !== null) {
      if (m[1]) res.push(m[1]);
    }
    console.log(res);

    I would seek and remove the square-bracket-delimited substrings and then do a match for all bounded strings of digits… something like this:

    var string = '1234 [ blabla 101112 ] 67890 [113141516 ] bla171819 212123';
    
    string.replace(/\[.+?\]/g, '').match(/\b\d+\b/g);
      // => ["1234", "67890", "212123"]
    

    May be you can do as follows;

    var str = "1234 [ blabla 101112 ] 67890 [113141516 ] bla171819 212123",
     result = str.match(/\d+(?=\s*\[|$)/g);
    console.log(result);