Skip to content

Instantly share code, notes, and snippets.

@gosoccerboy5
Last active July 8, 2021 21:29
Show Gist options
  • Save gosoccerboy5/ff5608beeb8d7af35df3705c45230606 to your computer and use it in GitHub Desktop.
Save gosoccerboy5/ff5608beeb8d7af35df3705c45230606 to your computer and use it in GitHub Desktop.
function stripBBCode(txt) {
return txt.replaceAll(/\[((?!\[).)*?\]/g, "");
}
alert(stripBBCode(prompt("What BBCode?")));
/*
View here {
https://regex101.com/r/5RtRdv/1
}
Context {
https://scratch.mit.edu/discuss/post/5215574/
}
About {
When viewing an ocular post (https://ocular.jeffalo.net/post/*), BBCode will show up in the page title.
This regex removes text between two [square brackets] (which is the form of a BBCode tag), but escape a tag if it has another pair of square brackets inside of it.
(https://en.scratch-wiki.info/wiki/BBCode#Advanced_BBCode)
}
*/
String stripBBCode(String txt) {
return txt.replaceAll(RegExp(r"\[((?!\[).)*?\]"), "");
}
void main() {
print(stripBBCode("""[b]Hello world![/b]
[[]This is an escaped tag]"""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment