This kind of stringvar d = "1[ddd]sfdsaf[ccc]fdsaf[bbbb]";
I want to get the string array between [and]
How to use a regular expression?
Does not include two parentheses
Currently I can only do it with parentheses
var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";
var patt = /\【[^\】]+\】/g;
d.match(patt)
Very simple, use zero-width assertion:
Only the zero-width positive lookahead assertion is used above. In fact, if it is not limited to JavaScript, it can also be written as
Zero-width assertions are divided into two categories and four types:
Forward zero-width assertion
Zero-width positive lookahead assertion
(?=exp)
Indicates that the expression after its own position can match exp, but does not match exp.
For example,
d+(?=999)
represents a number string ending with 999 (but the matching result does not contain 999)Zero-width assertion after positive review
(?<=exp)
(JavaScript not supported)Indicates that the expression that can match exp before its own position does not match exp.
For example,
(?<=999)d+
represents a number string starting with 999 (but the matching result does not contain 999)Negative zero-width assertion
Zero-width negative lookahead assertion
(?!exp)
Expression that indicates its own position cannot be followed by exp.
For example
d+(?!999)
means matching a string of numbers that does not end with 999Zero-width negative lookback assertion
(?<!exp)
(Not supported by JavaScript)Expression that indicates its own position cannot be preceded by exp.
For example
(?<!999)d+
means matching a string of numbers that does not start with 999Refer to @hack_qtxz's implementation using replace.
The following is the original answer:
And @Shuke’s answer is a bit repetitive, so I’m writing it in a different way.
Here is the original answer:
rrreeeQuote @cipchk to complete the code for you.