Skip to content

Instantly share code, notes, and snippets.

@bmcminn
Last active March 9, 2020 16:56
Show Gist options
  • Save bmcminn/584ce14d1670ca229fec050218c19af2 to your computer and use it in GitHub Desktop.
Save bmcminn/584ce14d1670ca229fec050218c19af2 to your computer and use it in GitHub Desktop.
JS WTF?! Unsuspecting Regex range selector captures numbers and certain punctuation? Assuming it's due to a unicode/ascii coercion in the JS interpreter. Works in Node 5+ and in Browser console.
"testing, waffles 1234567+=-_^@,./;:[]\/".replace(/[+-_]/g, '')
// > "testing waffles "
@bmcminn
Copy link
Author

bmcminn commented Jun 18, 2019

It has to do with the hyphen in the range [+-_], so yes, ASCII coercion in the JS interpreter REGEX engine appears to be the case here.

@bmcminn
Copy link
Author

bmcminn commented Mar 9, 2020

UPDATE: it's intended behavior as a range selector... [+-_] is a unicode coercion that matches all characters in that range.

Lesson learned: be explicit about your range capture values people, escape your hyphens \- if you want to capture hyphens :P

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