javascript - js regular expression to find the string between two characters
伊谢尔伦
伊谢尔伦 2017-05-19 10:37:52
0
5
818

This kind of string
var 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)
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(5)
大家讲道理
var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】"; 
var patt = /\【([^\】]+)\】/g; 
var arr = [];
d.replace(patt,function($0,$1){arr.push($1)});
给我你的怀抱

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:

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 exampled+(?!999) means matching a string of numbers that does not end with 999

Zero-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 999

淡淡烟草味

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
刘奇

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])
}
巴扎黑
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);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template