1

I'm using the newest version of the chrome browser on a mac book air with the newest OS. I'm trying to create a BBcode parser in javascript. I can easily parse one line code such as [b]a[/b] but when it's multiple lines, it doesn't find it. I did some research and found out javascript has some problems with the multiline (m) tag. I tried some alternative solutions and none of them seem to work. Here are some of my attempts:

var reg = new RegExp('\\[b\](.+)\\[\/b\]','igm');
var reg = new RegExp('\\[b\]([\s\S]+)\\[\/b\]','ig');
var reg = new RegExp('\\[b\]([^]+)\\[\/b\]','ig');
var reg = new RegExp('\\[b\]([\n|\r|.]+)\\[\/b\]','ig');

http://jsfiddle.net/zw6wF/1/

Any help would be appreciated.

Thanks

1 Answer 1

2

This must work:

 var reg = new RegExp('\\[b\\]([\\s\\S]+?)\\[/b\\]', 'ig');

or better:

var reg = new RegExp('\\[b]([\\s\\S]+?)\\[/b]', 'ig');

since you don't have to escape closing square brackets

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

5 Comments

I think the problem is that the + is too greedy.. his jfiddle sample shows multiple b tags in a single line. Otherwise I agree this should work
@Denomales: no the problem is that you must double escape all as in java with this syntax! But you are right a lazy quantifier is better.
@Denomales is right. I tried it and if there are multiple tags it only selects the first opening one and the last closing one ignoring everything in between
Yeah that works better for lines with multiple tags. And I didn't even notice the double escaped slashes +1
Or even /\[b]([\s\S]+?)\[\/b]/ig. I'd recommend /\[b]([^]+?)\[\/b]/ig but that doesn't work in some older IE versions.

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.