function removeTrailingSlash(url) {
return url.replace(/\/$/, "");
}
removeTrailingSlash("https://example.com/"); // "https://example.com"
removeTrailingSlash("https://example.com/docs/"); // "https://example.com/docs"Input boundary assertion: ^, $ / notebook
Input boundary assertion: ^, $
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
function isImage(filename) {
return /\.(?:png|jpe?g|webp|avif|gif)$/i.test(filename);
}
isImage("image.png"); // true
isImage("image.jpg"); // true
isImage("image.pdf"); // falsefunction isValidIdentifier(str) {
return /^[$_\p{ID_Start}][$_\p{ID_Continue}]*$/u.test(str);
}
isValidIdentifier("foo"); // true
isValidIdentifier("$1"); // true
isValidIdentifier("1foo"); // false
isValidIdentifier(" foo "); // falseconst variables = ["foo", "foo:bar", " foo "];
function toAssignment(key) {
if (isValidIdentifier(key)) {
return `globalThis.${key} = undefined;`;
}
// JSON.stringify() escapes quotes and other special characters
return `globalThis[${JSON.stringify(key)}] = undefined;`;
}
const statements = variables.map(toAssignment).join("\n");
console.log(statements);
// globalThis.foo = undefined;
// globalThis["foo:bar"] = undefined;
// globalThis[" foo "] = undefined;