Regex help needed

J

Jeff North

I need to split a string at every 2nd occurrence of a delimiter.

The string is:
CLAS|DIST|WKPL|ONLN|FLEX|FLXO|MIXD|OTHR|

and I need the output to be:
CLAS|DIST|<br>
WKPL|ONLN|<br>
FLEX|FLXO|<br>
MIXD|OTHR|

Does anyone know the right regex expression to use?

Many thanks
 
R

RobG

Jeff said:
I need to split a string at every 2nd occurrence of a delimiter.

The string is:
CLAS|DIST|WKPL|ONLN|FLEX|FLXO|MIXD|OTHR|

and I need the output to be:
CLAS|DIST|<br>
WKPL|ONLN|<br>
FLEX|FLXO|<br>
MIXD|OTHR|

Does anyone know the right regex expression to use?


var x = 'CLAS|DIST|WKPL|ONLN|FLEX|FLXO|MIXD|OTHR|';

alert( x.match( /\w{4}\|(\w{4})?/g ).join('|<br>') + '|' );

The trailing '|' causes a problem, both in the input string and for the
output chunks. It would be better (easier?) if your output was:

CLAS|DIST<br>
WKPL|ONLN<br>
FLEX|FLXO<br>
MIXD|OTHR
 
J

Jeff North

| Jeff North wrote:
| > I need to split a string at every 2nd occurrence of a delimiter.
| >
| > The string is:
| > CLAS|DIST|WKPL|ONLN|FLEX|FLXO|MIXD|OTHR|
| >
| > and I need the output to be:
| > CLAS|DIST|<br>
| > WKPL|ONLN|<br>
| > FLEX|FLXO|<br>
| > MIXD|OTHR|
| >
| > Does anyone know the right regex expression to use?
|
|
| var x = 'CLAS|DIST|WKPL|ONLN|FLEX|FLXO|MIXD|OTHR|';
|
| alert( x.match( /\w{4}\|(\w{4})?/g ).join('|<br>') + '|' );
|
| The trailing '|' causes a problem, both in the input string and for the
| output chunks. It would be better (easier?) if your output was:
|
| CLAS|DIST<br>
| WKPL|ONLN<br>
| FLEX|FLXO<br>
| MIXD|OTHR

Thanks for that. Works perfectly.
One of these days I'll get to learn regex :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top