user624385
user624385

Reputation: 373

Remove brakets and string and SPACE following

I have a variable string like such: [Text that changes]:SPACE: where the :SPACE: is actually a normal space. I need to remove the brackets and everything inside, plus the space following.

I have this:

      //<![CDATA[ 
  $(window).load(function(){
function stripParenthesis( node ) {
    if(node.length) {
        node.contents().each(function(index, child) {
            if( child.nodeType === 3 ) {
                child.nodeValue = child.nodeValue.replace(/\[.*?\]/g, '');
            }
            else {
                stripParenthesis( $(child) );
            }
        });
    }
}

stripParenthesis( $('div#brackets') );   });  //]]>

This manages to remove the brackets and everything inside. But what about the space following? I don't know how to adjust the following to make it include both the closing bracket and the space following...

.replace(/\[.*?\]/g, '')

Thanks for any help you can provide. Also, this function will hopefully work even if at some point there is no bracket?

Upvotes: 0

Views: 1713

Answers (2)

mason81
mason81

Reputation: 1750

enter code hereJust need to add the \s (whitespace char) at the end.

.replace(/\[.*?\]\s/g, '')

test it out here: http://gskinner.com/RegExr/

EDIT -- Complete Code:

//<![CDATA[ 
  $(window).load(function(){
function stripParenthesis( node ) {
    if(node.length) {
        node.contents().each(function(index, child) {
            if( child.nodeType === 3 ) {
                child.nodeValue = child.nodeValue.replace(/\[.*?\]\s/g, '');
            }
            else {
                stripParenthesis( $(child) );
            }
        });
    }
}

stripParenthesis( $('div#brackets') );   });  //]]>

Upvotes: 2

Danny
Danny

Reputation: 7518

This should work I believe

child.nodeValue = child.nodeValue.replace(/\[.*?\]\s/g, '');

Upvotes: 1

Related Questions