Javascript new RegExp (), reading samples from JSON

I am trying to create a bbcode filtering solution that works with both PHP and Javascript. I am working on javascript now. I'm having trouble getting a new RegExp constructor for pattern recognition in my json. Here is a small piece of code that reproduces the problem. Any understanding would be greatly appreciated!

bbcode.json

{"bbcode_regex": [
      {"regex": "<p>", "bbcode": ""},
      {"regex": "<\/p>", "bbcode": ""},
}

global.js

function html2bbcode(html) {
    var bbcode = html;

    jQuery.get("bbcode.json", {}, function(json) {
        for(var i in json.bbcode_regex) { 
            bbcode = bbcode.replace(new RegExp(json.bbcode_regex[i].regex, "g"), json.bbcode_regex[i].bbcode)
            console.log(new RegExp("/<p>/"));
        }
    }, 'json');

    return bbcode;
}

Please note that I am using FireBug and console.log RegExp exists only for experimenting / debugging. It seems that no matter what I entered as the first argument for the new RegExp, it only registers an empty object, for example {}. I'm not really worried about PHP right now, just javascript. Thanks!

+3
1

return bbcode;

undefined, , ajax. :

function html2bbcode(html, callback) {
    var bbcode = html;

    jQuery.get("bbcode.json", {}, function (json) {
        for (var i in json.bbcode_regex) { 
            bbcode = bbcode.replace(new RegExp(json.bbcode_regex[i].regex, "g"), json.bbcode_regex[i].bbcode);
        }
        callback(bbcode);
    }, 'json');

    return false;
}
+3

Source: https://habr.com/ru/post/1718720/


All Articles