tags:

views:

39

answers:

2

Hi guys...

I want to write a javascrpit code with .Split() that split a string with structure described below:

The input:

W1...Wn=>S1...||...Sm||Sj...Sk|Y1...Yn=>D1...Di||Dm...Dn|...

The Output:

W1...Wn=>S1...||...Sm||Sj...Sk

Y1...Yn=>D1...Di||Dm...Dn

...

I've seen the question that split this string: a=>aa|b=>b||b|c=>cc . but my question is general case of that question.

please help me...

Thanks...

A: 

Same method.

.split(/\|(?=\w+=>)/);
Delan Azabani
That will only work then the => is preceded by a single character, which is not the case here.
Tgr
Now I've fixed it to meet your needs; I replaced `\w` with `\w+`.
Delan Azabani
+1  A: 

match is easier to use here:

'W1W2W3=>S1S2S3||S4S5||S6S7S8|Y1Y2Y3=>D1D2D3||D4D5|D6'.match(/(?:\w|=>|\|\|)+/g);
// ["W1W2W3=>S1S2S3||S4S5||S6S7S8", "Y1Y2Y3=>D1D2D3||D4D5", "D6"]
Tgr