1

I have created a simple wrapper shortcode in my wordpress theme.

And am using ed.onBeforeSetContent and ed.onPostProcess to switch back and forth from

shortcode [wrap vars="whatever"]my cool content[/wrap] in text view to

html <div class="wrap">my cool content</div> in visual view

This works great with this code.

_get_wrap : function(co) {
    return co.content.replace(/\<div(.*?)?\>(?:(.+?)?\<\/div\>)?/g, function(a,b,c) {               
        var arr = b.replace(/['"]/g,'').split(' ');
        var params = new Object();
        for (var i=0;i<arr.length;i++){
            var p = arr[i].split('=');
            params[p[0]] = p[1];
        }
        if ( params['class'] == "wrap" )
            return '[wrap'+b+']'+c+'[/wrap]';
        return a;
    });
}

Unfortunately, if there is a return or new line within the content in between the opening and closing shortcode, it fails

How can I keep the c content intact with the code above when there are line breaks?

simonmorley
  • 2,810
  • 4
  • 30
  • 61
cheez la weez
  • 368
  • 1
  • 10
  • 1
    This is really quite difficult to take in. Perhaps consider reformatting your question so it's easy for your peers to read. – simonmorley Jul 02 '13 at 18:16
  • I agree with @simonmorley. I believe I answered the question correctly below, but you would have been better of boiling this question down to a very simple example that demonstrates the problem (not matching across newlines). – Ethan Brown Jul 02 '13 at 18:19

1 Answers1

2

The . wildcard does NOT match newlines. To match newlines, you have to use the following: [^]:

_get_wrap : function(co) {
    return co.content.replace(/\<div([^]*?)?\>(?:([^]+?)?\<\/div\>)?/g, function(a,b,c) {               
        var arr = b.replace(/['"]/g,'').split(' ');
        var params = new Object();
        for (var i=0;i<arr.length;i++){
            var p = arr[i].split('=');
            params[p[0]] = p[1];
        }
        if ( params['class'] == "wrap" )
            return '[wrap'+b+']'+c+'[/wrap]';
        return a;
    });
}
Ethan Brown
  • 26,892
  • 4
  • 80
  • 92