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 hidden or 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"] |
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.
I'm actually more interested in the use of Unicode FFFF as a temporary character, honestly.
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
FWIW, regex lookbehinds would make this cake, but we're in JavaScript-land here.