Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created July 24, 2012 17:06
Show Gist options
  • Save cowboy/3171233 to your computer and use it in GitHub Desktop.
Save cowboy/3171233 to your computer and use it in GitHub Desktop.
JavaScript: split on :, handling escaped \:
// Split colon-separated parts, unescaping (but not splitting on) any \:
function split(str) {
return str.replace(/\\:/g, '\uFFFF').split(':').map(function(part) {
return part.replace(/\uFFFF/g, ':');
});
}
// Eg.
split("foo:bar\\:baz\\:boo:qux") // ["foo", "bar:baz:boo", "qux"]
@cowboy
Copy link
Author

cowboy commented Jul 24, 2012

FWIW, regex lookbehinds would make this cake, but we're in JavaScript-land here.

@brianarn
Copy link

Don't know if it'd necessarily be preferred, but this would work:

function split(str) {
  return str.replace(/([^\\]):/g, '$1\uFFFF').replace(/\\:/g, ':').split('\uFFFF');
}

The only exception I see here is if the string leads with a colon, that'll get missed in the split. But, it has the upshot of not depending on map.

@cowboy
Copy link
Author

cowboy commented Jul 24, 2012

I'm actually more interested in the use of Unicode FFFF as a temporary character, honestly.

@MattSurabian
Copy link

It all depends on how sure you are of the cleanliness of your array data. This option avoids the injection of the Unicode and attempts to use a look ahead instead... Might not really work out any better though.

function split(str) {
   return str.replace(/\\:/g,':\\').split(/:(?!\\)/).map(function(part){
    return part.replace(/:\\/g,':');
   });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment