Sam Aniston Sam Aniston - 2 months ago 10
Javascript Question

JavaScript regex get all numbers but exlude all between brackets

I have string:

1234 [ blabla 101112 ] 67890 [113141516 ] bla171819 212123


I need to get only numbers inside of space symbols (or maybe start/end of line), but I don't need to get any symbols, that placed between square brackets [ ] in my string now.
The correct result should be:
1234 AND 67890 AND 212123

I've tried to write such JavaScript regex:

/(?!\[(.*?)\])((\s|^)(\d+?)(\s|$))/ig


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

Answer

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);

\d+(?=\s*\[|$)

Regular expression visualization

Debuggex Demo

Comments