The snippet library.
Small, practical examples. Search by task or narrow things down below.
Check whether text ends with a suffix
JavaScript: Use endsWith for a literal case-sensitive ending.
JavaScript✓ Tested
Check whether text starts with a prefix
JavaScript: Use startsWith for a literal case-sensitive prefix comparison.
JavaScript✓ Tested
Compare canonically equivalent Unicode text
JavaScript: Normalize to NFC before comparing equivalent composed and decomposed text.
JavaScript✓ Tested
Count Unicode code points in a string
JavaScript: String iteration combines surrogate pairs, unlike the UTF-16 length property.
JavaScript✓ Tested
Extract text by start and end positions
JavaScript: String slice uses an inclusive start and exclusive end measured in UTF-16 code units.
JavaScript✓ Tested
Extract the text after the last delimiter
JavaScript: Use lastIndexOf when earlier occurrences should remain part of the prefix.
JavaScript✓ Tested
Normalize identifier case consistently
JavaScript: Use toLowerCase for a simple case-insensitive ASCII identifier lookup.
JavaScript✓ Tested
Repeat a string a fixed number of times
JavaScript: String.repeat requires a nonnegative finite repetition count.
JavaScript✓ Tested
Transform each regex match with a callback
JavaScript: A replacement callback computes the replacement separately for each match.
JavaScript✓ Tested
Trim and discard blank lines
JavaScript: Split a text block, trim each line, and keep nonempty values.
JavaScript✓ Tested