V
V
Vadim Ushakov2021-11-12 06:54:59
JavaScript
Vadim Ushakov, 2021-11-12 06:54:59

How should the pattern for the string "[a][1][b][2]" look like?

There is a need to write a regular expression to extract data from brackets, 4 brackets in a row. This form does not work:

(/\[([^\]]+)\]{4,4}$/g).exec("[1][2][3][4]")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2021-11-12
@Nightmare1

const str = "[a][1][b][2]";
console.log(str.substr(1, str.length - 2).split('][')); // ['a', '1', 'b', '2']

const str = "    [a]  [1]       [b]      [2]       ".trim();
console.log(str.substr(1, str.length - 2).split(/\]\s*\[/)); // ['a', '1', 'b', '2']

A
Alexander Karabanov, 2021-11-13
@karabanov

And if so: https://regex101.com/r/yO6yOg/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question