Skip to content

Instantly share code, notes, and snippets.

@fhfaa
Last active December 21, 2015 12:25
Show Gist options
  • Save fhfaa/6061847 to your computer and use it in GitHub Desktop.
Save fhfaa/6061847 to your computer and use it in GitHub Desktop.
String Annotations @Schneller
(function (str, notes, wrap) {
var IDX = 0, TXT = 1,
lines = (Array(str.length + 1)).join(' ').split('');
notes = notes.map(function (n) {
return [+n.replace(/\D[\s\S]*$/, '') || 0, n.replace(/^\d+\s*/, '')];
});
notes.sort(function (a, b) {
return a[IDX] === b[IDX] ? 0 : (+a[IDX] < +b[IDX] ? 1 : -1);
}).forEach(function (n) {
lines[n[IDX]] = '|';
});
lines = lines.join('');
return [
(wrap ? '<pre><code>' : ''),
str,
lines.replace(/\|/g, '^'),
lines,
notes.map(function (n) {
return lines.substr(0, n[IDX]) + '+-- ' + (n[TXT] || '');
}).join('\n'),
(wrap ? '</code></pre>' : '')
].join('\n');
}(
'/^[^A-Z]+\\d{1-4}$/',
[
'1 Beginning of string',
'3 Anything but a regular capital letter',
'8 1 or more times (--> 1 or more non-caps)',
'10 Digit (0-9)',
'12 1-4 Times (--> 1-4 digits)',
'16 End of string'
]
));
/* Prints:
/^[^A-Z]+\d{1-4}$/
^ ^ ^ ^ ^ ^
| | | | | |
| | | | | +-- End of string
| | | | +-- 1-4 Times (--> 1-4 digits)
| | | +-- Digit (0-9)
| | +-- 1 or more times (--> 1 or more non-caps)
| +-- Anything but a regular capital letter
+-- Beginning of string
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment