如何获得输入中使用的一组简码?

How can I get an array of shortcodes used in my input?

我有一个为 wordpress 后端创建的插件。在一个部分中,用户可以在文本框中输入文本。我想更新屏幕顶部的列表,其中包含他们每次按下键时输入的所有简码及其值。

为此,我编写了如下代码用于测试:

$("#BBPlugin-Pages").on("keyup",".BBtextArea",function(e){

    var match = wp.shortcode.next("code", _2upstring, 2);
        if ( ! match ) {
            return;
        }

        else console.log(match.content);
});

所以现在,如果我输入 [code]words[/code] ,它会 return 当我输入任何内容时,但是,如果我稍后在文本框中添加另一个简码 [code] word2[/code] ,它也不会被 returned,只有第一个简码被 returned。有没有办法return两者兼顾?

请看here

wp.shortcode.next(标签、文字、索引)

Return 类型:对象 具有一些有用属性的容器对象:

index:匹配发生的索引,因此您可以使用匹配的索引加 1 轻松匹配下一个简码。

var index = 2; // yours initial value - don't know why 2
$("#BBPlugin-Pages").on("keyup",".BBtextArea",function(e){

var match = wp.shortcode.next("code", _2upstring, index);
    if ( ! match ) {
        return;
    }

    else {
        index = match.index + 1;
        console.log(match.content);
    }

})

无法测试 - 但想法是保存当前匹配项的索引。并从下一个字符开始新的搜索。

P.S。如果有人可以测试这个 - 如果 smth 是错误的,请编写正确的代码示例。

更新:全部记录下来。 (应该可以,但不能测试 w/o 环境 - 你应该只注意这个想法)

    var index = 2; // yours initial value - don't know why 2
    var all = []; 
$("#BBPlugin-Pages").on("keyup",".BBtextArea",function(e){

var match = wp.shortcode.next("code", _2upstring, index);
    if ( ! match ) {
        return;
    } else {
        index = match.index + 1;
        all.push( match.content );
        console.log(all); // as array
        all.forEach(function(x){ console.log(x); }); // one at a time
    }

})