Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Created August 19, 2019 19:45
Show Gist options
  • Save dfkaye/0030d9db09b541d5d84a833036ad9f11 to your computer and use it in GitHub Desktop.
Save dfkaye/0030d9db09b541d5d84a833036ad9f11 to your computer and use it in GitHub Desktop.
Shorthand regex [A-z] to match all ASCII letters, backtick, caret, underscore, opening & closing square brackets...
// 19 August 2019
// Just learned something new:
// Shorthand regex [A-z] will find all ASCII letters, lower- and uppercase.
// And will also find backtick, caret, underscore, opening and closing square brackets...
// Who knew?
// @jessitron => https://twitter.com/jessitron/status/1163481184413913088
var re = /[A-z]+/;
var tests = `09aZ~!@#$%^&*()_+\`-={}|[]\:;"'<,>.?/'"`.split('');
var results = tests
.map(char => {
return {char, match: re.test(char) }
})
.sort((a, b) => {
// shift truthy matches to the lower indexes of the array.
return a.match ? -1 : b.match ? 1 : 0
});
var report = JSON.stringify(results, '', 2);
console.log(report);
/*
[
{
"char": "a",
"match": true
},
{
"char": "Z",
"match": true
},
{
"char": "^",
"match": true
},
{
"char": "_",
"match": true
},
{
"char": "`",
"match": true
},
{
"char": "[",
"match": true
},
{
"char": "]",
"match": true
},
{
"char": "0",
"match": false
},
{
"char": "9",
"match": false
},
{
"char": "~",
"match": false
},
{
"char": "!",
"match": false
},
{
"char": "@",
"match": false
},
{
"char": "#",
"match": false
},
{
"char": "$",
"match": false
},
{
"char": "%",
"match": false
},
{
"char": "&",
"match": false
},
{
"char": "*",
"match": false
},
{
"char": "(",
"match": false
},
{
"char": ")",
"match": false
},
{
"char": "+",
"match": false
},
{
"char": "-",
"match": false
},
{
"char": "=",
"match": false
},
{
"char": "{",
"match": false
},
{
"char": "}",
"match": false
},
{
"char": "|",
"match": false
},
{
"char": ":",
"match": false
},
{
"char": ";",
"match": false
},
{
"char": "\"",
"match": false
},
{
"char": "'",
"match": false
},
{
"char": "<",
"match": false
},
{
"char": ",",
"match": false
},
{
"char": ">",
"match": false
},
{
"char": ".",
"match": false
},
{
"char": "?",
"match": false
},
{
"char": "/",
"match": false
},
{
"char": "'",
"match": false
},
{
"char": "\"",
"match": false
}
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment