2

I'm writing BBCode to HTML praser via JavaScript. I successed with [b], [i], [img], ... tags, but with [list] tag, I dont know how to do, because it's element maybe 2, 3, n,... Can you help me convert from:

[list]
item 1
item 2
item 3
[/list]

to

<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<ul>

Thank you very much.

4
  • 3
    what have you tried? Commented Sep 24, 2012 at 2:11
  • How is your parser implemented? LALR? Using regular expressions, a state machine of your own making, or a horrible combination of indexOf/split, and substring? Commented Sep 24, 2012 at 2:15
  • @Dai What's wrong with split? That seems relevant here. Commented Sep 24, 2012 at 2:21
  • split causes the allocation and copying of string contents, which can have a considerable performance impact when processing lots of strings (such as in a logfile parser), and then there's the cost of GC to consider too. Any task that uses split can be performed using a regex or state-machine with considerably better performance. Commented Sep 24, 2012 at 2:54

1 Answer 1

1

If you are using that horrid combination of indexOf, split and substring @Dai is talking about, something similar to the following will do it.

var start = this.value.indexOf("[list]"),
    ending = this.value.indexOf("[/list]"),
    lines = this.value.substring(start + 7, ending - 1).split("\n");

//perform loop on `lines`:
//    "<li>" + lines[i] + "</li>"

Essentially, this will get the start position of [list] and [/list] and then get the lines in between. You'll notice that in the substring(), I say start+7 and ending-1. This can be explained by the fact that the start position of your actual lines will be at the index of [list] + the actual length of the string [list] + the enter character. The end subtracts one to remove that enter character. A better way to do this would be to simply add six to start instead of seven, subtract zero and then just get rid of any empty array values, however, either way is fine depending on the exact syntax you're requiring for your users.

Keep in mind that the lines variable will hold all of the lines entered between the two list bbcodes meaning you have to loop through them and then wrap them with <li></li> tags. Also, I suggest validating multiple things such as making sure that start > ending and performing this multiple times so you know every occurrence of [list] instead of just the first.

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

4 Comments

My comment was facetious in nature, but the solution you've devised is brittle. Consider the invalid input string "[list][list][/list]" which passes the validation you described. There is also the case where BBCode is escaped, such as in [code] blocks, e.g. "[code][list][/code]", which would still be converted by your code.
@Dai I hope you've read my whole answer. In no way am I here to freely code for the OP. I warned him of validating "multiple things" in my post above.
Yes, but the only way to validate the input BBCode is to parse it in the first place.
@Dai I hope you're aware of the situation. The OP is asking how (s)he would go about doing what s(he) wants to do. This is one way to go about doing it, but it's not complete nor is it secure. These are things I'm leaving to the OP to do. I'm not here to program for him/her.

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.