regex between two special characters

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

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



        
6条回答
  •  执笔经年
    2021-01-26 11:45

    You can get all blocks of text between { and } into an array with this:

    function getBracedText(input) {
        var re = /\{(.*?)\}/g, matches, output = [];
        while (matches = re.exec(input)) {
            output.push(matches[1]);
        }
        return(output);
    }
    
    var str = "Some text {tag} and more {text}";
    var results = getBracedText(str);
    // results == ["tag", "text"];
    

    Working demo: http://jsfiddle.net/jfriend00/DT4Km/

提交回复
热议问题