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)
大家讲道理2017-05-19 10:39:52
var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";
var patt = /\【([^\】]+)\】/g;
var arr = [];
d.replace(patt,function($0,$1){arr.push($1)});
给我你的怀抱2017-05-19 10:39:52
Very simple, use zero-width assertion:
var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";
d.match(/[^【]+(?=】)/g);
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:
(?=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.
var d= "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";
var patt = /\【([^\】]+)\】/g;
var result = d.replace(patt, (var d= "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";
var patt = /\【[^\】]+\】/g;
var result = d.match(patt).map(v => v.substr(1, v.length-2));
console.log(result);
, )=>',!' + + ',').split(',').filter(v=>-1 != v.indexOf('!')).map(v=>v.substr('1'));
console.log(result);
The following is the original answer:
And @Shuke’s answer is a bit repetitive, so I’m writing it in a different way.
var d= "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";
var patt = /\【([^\】]+)\】/g;
var matches;
var result = [];
while ( !!(matches = patt.exec(d)) ) {
result.push(matches[1]);
}
console.log(result);
Here is the original answer:
rrreee怪我咯2017-05-19 10:39:52
Quote @cipchk to complete the code for you.
var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";
var myregexp = /【([^】]+)/g;
var result;
while ((result = myregexp.exec(d)) != null) {
console.log(result[1])
}
巴扎黑2017-05-19 10:39:52
var myregexp = /【([^】]+)/g;
var match = myregexp.exec(subject);
while (match != null) {
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
match = myregexp.exec(subject);
}