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
1 2 3 |
|
大家讲道理2017-05-19 10:39:52
1 2 3 4 |
|
给我你的怀抱2017-05-19 10:39:52
Very simple, use zero-width assertion:
1 2 |
|
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
1 |
|
Zero-width assertions are divided into two categories and four types:
(?=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)
(?<=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)
(?!exp)
Expression that indicates its own position cannot be followed by exp.
For exampled+(?!999)
means matching a string of numbers that does not end with 999
(?<!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 999
淡淡烟草味2017-05-19 10:39:52
Refer to @hack_qtxz's implementation using replace.
1 2 3 4 5 6 7 |
|
The following is the original answer:
And @Shuke’s answer is a bit repetitive, so I’m writing it in a different way.
1 2 3 4 5 6 7 8 9 |
|
Here is the original answer:
rrreee怪我咯2017-05-19 10:39:52
Quote @cipchk to complete the code for you.
1 2 3 4 5 6 |
|
巴扎黑2017-05-19 10:39:52
1 2 3 4 5 6 7 8 |
|