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

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