search

Home  >  Q&A  >  body text

javascript - js regular expression to find the string between two characters

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

1

2

3

<code>var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";

var patt = /\【[^\】]+\】/g;

d.match(patt)</code>

伊谢尔伦伊谢尔伦2902 days ago912

reply all(5)I'll reply

  • 大家讲道理

    大家讲道理2017-05-19 10:39:52

    1

    2

    3

    4

    <code>var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";

    var patt = /\【([^\】]+)\】/g;

    var arr = [];

    d.replace(patt,function($0,$1){arr.push($1)});</code>

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:39:52

    Very simple, use zero-width assertion:

    1

    2

    <code class="javascript">var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";

    d.match(/[^【]+(?=】)/g);</code>

    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

    <code>(?<=【).+?(?=】)</code>

    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

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:39:52

    Refer to @hack_qtxz's implementation using replace.

    1

    2

    3

    4

    5

    6

    7

    <code>var d= "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";

    var patt = /\【([^\】]+)\】/g;

    var result = d.replace(patt, (<pre class="brush:php;toolbar:false;toolbar:false;"><code>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);</code></pre>, )=>',!' +  + ',').split(',').filter(v=>-1 != v.indexOf('!')).map(v=>v.substr('1'));

    console.log(result);</code>

    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

    <code>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);</code>

    Here is the original answer:

    rrreee

    reply
    0
  • 怪我咯

    怪我咯2017-05-19 10:39:52

    Quote @cipchk to complete the code for you.

    1

    2

    3

    4

    5

    6

    <code>var d = "1【ddd】sfdsaf【ccc】fdsaf【bbbb】";

    var myregexp = /【([^】]+)/g;

    var result;

    while ((result = myregexp.exec(d)) != null) {

        console.log(result[1])

    }</code>

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:39:52

    1

    2

    3

    4

    5

    6

    7

    8

    <code>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);

    }</code>

    reply
    0
  • Cancelreply