regex between two special characters

后端 未结 6 1174
花落未央
花落未央 2021-01-26 11:12

Is there a way to replicate this PHP snippet in JQuery or Javascript...



        
6条回答
  •  有刺的猬
    2021-01-26 11:35

    Something like this should do:

     var re = /{[^}]*}/g;
     var s = '{foo},bar,{fum}';
    
     alert(s.match(re)); // ['{foo}','{fum}']
    

    but you may not want the "{}" included.

    var re = /{([^}]*)}/g;
    var s = '{foo},bar,{fum}';
    
    var a = [];
    var b;
    while (b = re.exec(s)) {
      a.push(b[1]);
    }
    
    alert(a); // ['foo','fum'];
    

    There's bound to be a way to do it with a single regular expression and no loop.

提交回复
热议问题