Created
July 24, 2012 17:06
-
-
Save cowboy/3171233 to your computer and use it in GitHub Desktop.
JavaScript: split on :, handling escaped \:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"] |
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
I'm actually more interested in the use of Unicode FFFF as a temporary character, honestly.