Code snippets
Change letter case with regex
Section titled “Change letter case with regex”Tokens for the replacement field of find and replace.
| Token | Effect |
|---|---|
\U |
uppercase until \E |
\L |
lowercase until \E |
\u |
uppercase the next character |
\l |
lowercase the next character |
They work in JetBrains IDEs, VS Code, Notepad++, GNU sed and Perl. JavaScript String.replace has none; use a callback. Example: find (\w+), replace \u\L$1 (\1 in sed), hELLO becomes Hello.
Source: Replacement Text Case Conversion.
Load a script without jQuery
Section titled “Load a script without jQuery”A replacement for $.getScript. In ES modules prefer import().
const getScript = url => new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = url; script.onload = () => resolve(script); script.onerror = () => reject(new Error(`Failed to load script: ${url}`)); document.head.appendChild(script);});
await getScript('https://example.com/library.js');CLI arguments with yargs
Section titled “CLI arguments with yargs”npm install yargsimport yargs from 'yargs';import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv)) .option('o', { alias: 'option', type: 'boolean', default: false }) .parse();For simple cases Node.js has the built-in util.parseArgs. yargs docs.
Libraries
Section titled “Libraries”| Name | Description | Code | Language |
|---|---|---|---|
| Knex.js | SQL query builder | knex/knex | Node.js |
| Faker | Fake data generator | faker-js/faker | Node.js |

