how to split a string in js with some exceptions

I have this string:


I need a JavaScript function that will split the string by every , but only those that don't have a \ before them

How can this be done?



Here's the shortest thing I could come up with:

'a\\,bcde,fgh,ijk\\,lmno,pqrst\\,uv'.replace(/([^\\]),/g, '$1\u000B').split('\u000B')

The idea behind is to find every place where comma isn't prefixed with a backslash, replace those with string that is uncommon to come up in your strings and then split by that uncommon string.

Note that backslashes before commas have to be escaped using another backslash. Otherwise, javascript treats form \, as escaped comma and produce simply a comma out of it! In other words if you won't escape the backslash, javascript sees this: a\,bcde,fgh,ijk\,lmno,pqrst\,uv as this a,bcde,fgh,ijk,lmno,pqrst,uv.


Since regular expressions in JavaScript does not support lookbehinds, I'm not going to cook up a giant hack to mimic this behavior. Instead, you can just split() on all commas (,) and then glue back the pieces that shouldn't have been split in the first place.

Quick 'n' dirty demo:

var str = 'a\\,bcde,fgh,ijk\\,lmno,pqrst\\,uv'.split(','), // Split on all commas
    out = []; // Output

for (var i = 0, j = str.length - 1; i < j; i++) { // Iterate all but last (last can never be glued to non-existing next)
    var curr = str[i]; // This piece
    if (curr.charAt(curr.length - 1) == '\\') { // If ends with \ ...
        curr += ',' + str[++i]; // ... glue with next and skip next (increment i)
    out.push(curr); // Add to output

Another ugly hack around the lack of look-behinds:

function rev(s) {
    return s.split('').reverse().join('');

var s = 'a\\,bcde,fgh,ijk\\,lmno,pqrst\\,uv';

// Enter bizarro world...
var r = rev(s);

// Split with a look-ahead
var rparts = r.split(/,(?!\\)/);

// And put it back together with double reversing.
var sparts = [ ];

for(var i = 0; i < sparts.length; ++i)
    $('#out').append('<pre>' + sparts[i] + '</pre>');


I don't think I'd do this in real life but it works even if it does make me feel dirty. Consider this a curiosity rather than something you should really use.


In case if need remove backslashes also:

var test='a\\.b.c';
var result = test.replace(/\\?\./g, function (t) { return t == '.' ? '\u000B' : '.'; }).split('\u000B');
//result: ["a.b", "c"]

You can use regex to do the split.

Here is the link to regex in javascript

Here is the link to other post where the author have used regex for split Javascript won't split using regex

From the first link if you note you can create a regular expression using

?!n Matches any string that is not followed by a specific string n



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.