简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM