Collection · 51 utilities

String utilities

Text can be code points, graphemes or normalized words. Choose the contract before applying a transformation.

Capitalize a string

Uppercase the first Unicode code point and preserve the remaining text.

function capitalize(value) { const [first, ...rest] = Array.from(value); return first ? first.t…string

Reverse a string

Reverse Unicode code points without splitting surrogate pairs.

function reverse(value) { return Array.from(value).reverse().join(''); }string

Check a palindrome

Compare a string with its code-point reversal.

function palindrome(value) { return value === Array.from(value).reverse().join(''); }string

Create a simple URL slug

Normalize a Latin-text label into lowercase ASCII words separated by hyphens.

function slugify(value) { return value.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowe…string

Convert snake case to camel case

Uppercase a lowercase ASCII letter immediately after an underscore.

function camelCase(value) { return value.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase…string

Remove duplicate lines

Keep the first occurrence of every exact line while normalizing CRLF to LF.

function uniqueLines(value) { return [...new Set(value.split(/\r?\n/))].join('\n'); }string

Check if a URL is absolute

Detect a scheme such as https: at the start of a URL string.

function isAbsoluteUrl(url) { return /^[a-z][a-z0-9+.-]*:/i.test(url); }string

Check if a path is relative

Return true unless the path starts with a slash or backslash, optionally after a drive or scheme.

function isRelative(path) { return !/^([a-z]+:)?[\\/]/i.test(path); }string

Check if two strings are anagrams

Compare the sorted, lowercased characters of both strings.

function areAnagram({ first, second }) { const sorted = text => Array.from(text.toLowerCase()).…string

Generate a hash of a string

Compute the 32-bit hash Java’s String.hashCode uses.

function hashString(text) { return text.split('').reduce((hash, char) => (Math.imul(31, hash) +…string

Strip ANSI codes from a string

Remove terminal color and cursor escape sequences.

function stripAnsi(text) { return text.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0…string

Swap case of characters in a string

Uppercase lowercase letters and lowercase the rest.

function swapCase(text) { return Array.from(text).map(char => char === char.toLowerCase() ? cha…string

Convert a string to Pascal case

Join alphanumeric words, each starting with an uppercase letter.

function toPascalCase(text) { return (text.match(/[a-zA-Z0-9]+/g) || []).map(word => `${word.ch…string

Get the file extension from a file name

Return the text after the last dot, or an empty string.

function fileExtension(name) { const dot = name.lastIndexOf('.'); return dot > 0 ? name.slice(d…string

Get the file name from a URL

Take the last path segment, ignoring the query and fragment.

function fileNameFromUrl(url) { const path = url.split(/[?#]/)[0]; return path.slice(path.lastI…string

Normalize file path slashes

Collapse runs of slashes and backslashes into single forward slashes.

function normalizePath(path) { return path.replace(/[\\/]+/g, '/'); }string

Remove spaces from a string

Delete every whitespace character, including tabs and line breaks.

function removeSpaces(text) { return text.replace(/\s/g, ''); }string

Repeat a string

Concatenate a string with itself a given number of times.

function repeat({ text, times }) { return text.repeat(times); }string

Mask the first characters of a string

Replace the first count characters with a mask character, keeping the length.

function maskStart({ text, count, mask }) { return text.slice(count).padStart(text.length, mask…string

Convert a string to camel case

Remove separators and uppercase the character after each one.

function toCamelCase(text) { return text.trim().replace(/[-_\s]+(.)?/g, (_, char) => char ? cha…string

Decapitalize a string

Lowercase the first code point and keep the rest.

function decapitalize(text) { const [first = '', ...rest] = Array.from(text); return first.toLo…string

Escape HTML special characters

Replace & < > " and ' with HTML entities.

function escapeHtml(text) { return text.replace(/[&<>"']/g, char => ({ '&': '&amp;', '<': '&lt;…string

Format a string

Replace {0}, {1}… placeholders with values in order.

function format({ template, values }) { return values.reduce((text, value, i) => text.replaceAl…string

Trim a character from both ends

Remove a repeated character from the start and the end only.

function trimCharacter({ text, char }) { const escaped = char.replace(/[.*+?^${}()|[\]\\]/g, '\…string

Truncate a string at full words

Cut at the last space that fits, then add a suffix.

function truncateWords({ text, max, suffix }) { if (text.length <= max) return text; const cut …string

Unescape HTML special characters

Turn &amp; &lt; &gt; &quot; and &#39; back into characters in one pass.

function unescapeHtml(text) { return text.replace(/&(?:amp|lt|gt|quot|#0*39);/g, entity => ({ '…string

Remove empty lines of a text document

Drop lines that are empty or whitespace only.

function removeEmptyLines(text) { return text.split(/\r?\n/).filter(line => line.trim() !== '')…string

Decode numeric HTML entities

Turn &#233; and &#xE9; style references into characters.

function decodeNumericEntities(text) { return text.replace(/&#(x[0-9a-f]+|\d+);/gi, (entity, co…string

Try a function in the local code lab, then read its limits before using it in an application.

Type two or more characters. Try “duplicate”, “webhook” or “retry”.