开发者

Javascript String Parsing

开发者 https://www.devze.com 2023-03-25 16:37 出处:网络
I am trying to parse a string in this format [something](something s开发者_高级运维omething) [something](something something)

I am trying to parse a string in this format

[something](something s开发者_高级运维omething) [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("+++"))


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?


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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消