2

I am trying to remove BBCode with attributes and content between those tags. I'm using this regular expression that I got here from here. I also tried other regex I found on stackoverflow but they didn't work for me, just the one I copy here is the closest.

([[\/\!]*?[^\[\]]*?])

I added a . before *?]) and it maches the text between the tags but also matches pokemon and I don't want that.

**Regex**: ([[\/\!]*?[^\[\]].*?])

**Text**: I'm a pokemon master and I like
[TAG] this [/TAG] pokemon [TAG] and this [/TAG] text...

I use this web to test regex http://regexpal.com/

Can anyone help me?

Thanks in advance.

3 Answers 3

3
str = str.replace(/\[(\w+)[^\]]*](.*?)\[\/\1]/g, '');

jsFiddle.

Sign up to request clarification or add additional context in comments.

Comments

3

This is what you want:

.replace(/\[(\w+)[^\]]*](.*?)\[\/\1]/g, '$2');

JavaScript demo

Basically you catch the value between tags and then replace the whole string with that value.

Using a regex to do this isn't a very clean way of doing it though...

Sorry Alex but you didn;t read it seems.

2 Comments

It's almost the same... hehe. I'm getting a feed from a forum and I'm using javascript only to develop something. THanks.
What is [^w]? How would that allow for arbitrary attributes?
1

This should do:

\[(\w+).*?\].*?\[/\1\]

This will look for a closing tag matching the opening tag - and also accept attributes on the opening tag. The JavaScript code should then be:

str = str.replace(/\[(\w+).*?\].*?\[\/\1\]/, "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.