so basically, what I've got is a handful of BBC text like such:

[tree]title0
[node]title1
title2
[node]title3
title4
title5[/node][/node]
title6
title7[/tree]

using JS, how could I parse this to get this into this??
[ title0, [ title1, title2, [ title3, title4, title5 ] ], title6, title7 ]

what I intend to do is use functions recursively and return the html-text of the parsed nodes

reason for recursive functions is for telling which nodes (single or sub-tree) are the last nodes in each level
(I need a different image displayed so it doesn't draw a line where there should be no line)

thanx for any and all help :)

EDIT: note: title0 and title1 are on the same level (along with titles 6 and 7)
title0 is a single node
title1 is an expandable node with 2 sub-nodes (title2 and title3)

ok, so for testing, I've switched to building a python parser and got something working at a primitive level...

post = 'node0<br>node1<br>[node]node2<br>node2-0<br>node2-1<br>[node]node2-2<br>node2-2-0[/node]node2-3[/node]<br>[node]node3<br>node3-0<br>[/node]'

nlevel = 0
for level,data in enumerate(post.split('[node]')):
    for T,node in enumerate(data.split('[/node]')):
        if T>0: nlevel+=1
        singles = node.split('<br>')
        for N,single in enumerate(singles):
            if single=='': continue
            outer = (1 if len(singles)>1 else 0) if N==0 else 0
            print ''.join(['  ']*(level-nlevel-outer))+single

and here's the output:

>>> 
node0
node1
node2
  node2-0
  node2-1
  node2-2
    node2-2-0
  node2-3
node3
  node3-0

only problem is, that's as good as it gets...
I'm not sure how to perform a check for the last node on the current level.
(so it'll mark the last node instead of drawing a line through the rest of the level for the sub-nodes)

Member Avatar for diafol
var str = '[tree]title0[node]title1 title2 [node]title3 title4 title5[/node][/node] title6 title7[/tree]';

var out = str.replace(/\[\S{4}\]/g, '[');
var out = out.replace(/\[\/\S{4}\]/g, ']');

//output to div with id="ans"
document.getElementById('ans').innerText = out;

Caveat
I think js replace has trouble with multilines.
Aso, if there is more to the initial string than this, e.g. more bbcode, perhaps you need to replace literals not as above.

var out = str.replace(/\[tree\]/g, '[');
var out = out.replace(/\[\/tree\]/g, ']');
var out = out.replace(/\[node\]/g, ']');
var out = out.replace(/\[\/node\]/g, ']');

guess I shoulda been a bit more clear on what I wanted... sry...
back then I was thinking to create and parse an array of arrays based on their level...

but now that I've done python tests, I realize I don't need that and can do things a bit more efficiently...

all I'm left with is baing able to tell if the current level needs a line or a space drawn.
otherwize I'll have a tree that looks like:

node0
|-node1
| |-node2
| |-node3
| | |-node4
| | |-node5

instead of

node0
|-node1
  |-node2
  |-node3
    |-node4
    |-node5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.