mossman mossman - 1 month ago 16
HTML Question

Remove square brackets and their contents using jQuery + regexes

Using an example I've found here, I am trying to remove occurrences of text that are surrounded by these

[
]
square brackets.

I have many occurrences of
[sample-text]
in my html and want to remove the whole thing, including the brackets.

I've tried this but it doesn't work -- nothing is being replaced:

var replaced = $("body").html().replace(/^\[.*\]$/g,'');
$("body").html(replaced);


I am trying to match the start using
[
and the end with
]
and remove the inner content but I am getting nowhere.

Answer
var replaced = $("body").html().replace(/\[.*\]/g,'');
$("body").html(replaced);
Comments