3

I'm trying to create a bbcode filtering solution that works with both PHP and Javascript. Right now I'm working on the javascript. I'm having trouble getting the new RegExp constructor to recognize the patterns in my json. Here's a small bit of sample code that reproduces the issue. Any insight 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;
}

Note that I'm using FireBug and the console.log RegExp is there just for experimenting / debug purposes. It seems like no matter what I put in as the first argument for the new RegExp it only logs an empty object like {}. I'm no so much worried about the PHP right now, just the javascript. Thanks!

3
  • 2
    Same question here every single day. I wish people had an understanding of AJAX before they start writing it. Commented Sep 28, 2009 at 15:33
  • 1
    Well, now that we're here, care to clarify or link to a similar question that will help me with my issue? Commented Sep 28, 2009 at 15:37
  • You need to escape the "\" in the JSON-code, as they are simple strings. The "/" does not need to be escaped when using the new Regex() constructor, so in this simple case you don't get any problems. Commented Sep 28, 2009 at 17:41

1 Answer 1

3

The line

return bbcode;

will return undefined, it will initialized later, when ajax request was done. Use callback:

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Untrue. That line returned an unfiltered version of bbcode because the Regex object isn't instantiating properly. If I return false then I get "false" for the text that shows up on the page.
No aphelionz, Anatoliy is correct. You get unfiltered content back because html2bbcode() returns before the AJAX callback executes. You may also have a RegExp problem, but that doesn't mean Anatoliy is wrong.
Yes, you are right, it's return html, but it's the same as passed to function, it means, than function doing nothing with passed html. Expected behavior - replace with regex, fetched via AJAX.
Ah, my mistake! I still think I have a RegExp problem though.
I've tested and was not found any problems with regex: 'aa<p>'.replace(new RegExp('<p>', "g"), ''); returns 'aa' as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.