This question already has answers here:
Regex to get string between curly braces
                            
                                (13个回答)
                            
                    
                4年前关闭。
        

    

我有一些像

var str="#{ZyDt8spGZOA.mI8uHaT7o47}+#{z3PENrRf0cn.mI8uHaT7o47}+#{lhUBSvCMPWu.mI8uHaT7o47}+#{YNpiWU7fw9m.mI8uHaT7o47}";


我只需要将内容放在大括号内即可。 (在上述情况下,长度为4的数组)。

我该如何实现?

最佳答案

    var found = [],
    rxp = /{([^}]+)}/g,
    str = "#{ZyDt8spGZOA.mI8uHaT7o47}+#{z3PENrRf0cn.mI8uHaT7o47}+#{lhUBSvCMPWu.mI8uHaT7o47}+#{YNpiWU7fw9m.mI8uHaT7o47}",
    mat;

    while( mat = rxp.exec( str ) ) {
          found.push(mat[1]);
    }

    alert(found);

10-02 02:43