Reputation: 373
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
Reputation: 1750
enter code here
Just 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
Reputation: 7518
This should work I believe
child.nodeValue = child.nodeValue.replace(/\[.*?\]\s/g, '');
Upvotes: 1