rubixibuc
rubixibuc

Reputation: 7427

Javascript String Parsing

I am trying to parse a string in this format

[something](something something) [something](something something)

and I want to break on every space that is not between a set of parenthesis?

I tried using js string.split with this as the regex /[^\(].*\s+.*[^\)]/g, but it doesn't work? Any suggestions appreciated :-)

EDIT: I don't want to post this as an answer, because I want to leave it open to comments but I finally found a solution.

var a = "the>[the](the the) the>[the](the the) the"
var regex = /\s+(?!\w+[\)])/
var b = a.split(regex)
alert(b.join("+++"))

Upvotes: 0

Views: 2416

Answers (3)

chjj
chjj

Reputation: 14602

This regex will do exactly what you asked, and nothing more:

'[x](x x) [x](x x)'.split(/ +(?![^\(]*\))/);

Upvotes: 0

riwalk
riwalk

Reputation: 14233

You are using the wrong tool for the job.

As was alluded to in this famous post, regular expressions cannot parse non-regular languages, and the "balanced parenthesis" problem cannot be described by a regular language.

Have you tried writing a parser instead?

EDIT:

It seems that you've finally clarified that nesting is not a requirement. In that case, I'd suggest gnur's solution.

Upvotes: 1

gnur
gnur

Reputation: 4733

Is your input always this consistent? If it is, it could be as simple as splitting your string on ') ['

If it isn't, is it possible to just take what is between [ and )? Or is there some kind of nesting that is going on?

Upvotes: 2

Related Questions