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);
});
Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.
split well-known symbol specifies the method that splits a string at the indices that match a regular expression. This function is called by the String. prototype. split() method.
To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With