cheez la weez
cheez la weez

Reputation: 368

REGEX - HTML to Shortcode breaking when inner content has line breaks

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?

Upvotes: 1

Views: 276

Answers (1)

Ethan Brown
Ethan Brown

Reputation: 27292

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;
    });
}

Upvotes: 2

Related Questions