Amarnath R Shenoy Amarnath R Shenoy - 2 months ago 7
Javascript Question

How can I split a string separated with pipe symbol using jQuery regex

I have a dynamically generated string which is basically a row with each value separated by a pipe symbol

|
, I need to separate it and insert them into a bunch of hidden fields .

This was almost what I needed to do, but not working for me, and I can only use (
~
or
|
) as special characters since my data may contain other characters.

Here is my code:

var data = "Val1@#|val2$%|val3(*|"; // dynamically generated

$.each(data.split(/\s*|\s+/), function(i, val) {
alert(val);
});

Answer

Use this:

var Data ="Val1@#|val2$%|val3(*|" //dynamically generated 
alert(Data);
$.each(Data.split(/\|/), function (i, val) {
     alert(val);
})

Working Fiddle: http://jsfiddle.net/nLdcr/