jquery转义数组中的方括号

时间:2021-04-08 21:42:27

i am trying to parse wordpress shortcodes with square brackets

我试图用方括号解析wordpress短代码

            <script src="jquery.js" charset="utf-8"></script>
            <style>


            </style>
            <textarea cols="40" rows="6" id="editor">
            [section] content [/section]
            </textarea>
            <div id="display"></div>

            <script>
            var fr = {
                "\\[section\\]": "<div class'section'>",
                "[/section]": "</div>",
                "content": "new content",
            };

            var re = $.map(fr, function (v, k) {
                return {
                    regex: new RegExp('\\b' + k + '\\b', 'g'),
                    value: v
                };
            });


            $("#editor").keyup(function(){
                        var post = $("#editor").val();
                        $('#display').html(post);
                        parse();
            });


            function parse()
            {
                    jQuery('#display').html(function (i, val) {
                    $.each(re, function (i, obj) {
                        val = val.replace(obj.regex, obj.value);
                    });
                    return val;
                });
            }

            $("#editor").keyup();

            </script>

i have tried escaping the brackets with double \ but no effect is there a way to escape them or alternatively a different way to parse them altogether

我已经尝试用双\来转义括号但是没有效果是有办法逃避它们或者另外一种方法来完全解析它们

1 个解决方案

#1


0  

Your slashes are /, they should be \ for escaping. Writing "\[stuff\]" should be just fine.

你的斜杠是/,它们应该是\逃脱。写“\ [stuff \]”应该没问题。

#1


0  

Your slashes are /, they should be \ for escaping. Writing "\[stuff\]" should be just fine.

你的斜杠是/,它们应该是\逃脱。写“\ [stuff \]”应该没问题。