Skip to content

Instantly share code, notes, and snippets.

Last active July 4, 2021 10:41
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dherman/6165867 to your computer and use it in GitHub Desktop.
What can you do with ES6 string template literals?

DOM selectors

var elements = query`.${className}`;


var message = l10n`Hello ${name}; you are visitor number ${visitor}:n!
You have ${money}:c in your account!`;

Customizable regexps

var match = input.match(re`\d+${separator}\d+`);

Multiline regexps

var parts = str.match(regex`
    ^                    # match at start of string only
    / (?<year> [^/]+ )   # capture top dir name as year
    / (?<month> [^/]+ )  # capture subdir name as month
    / (?<title> [^/]+ )  # capture file name without ext as title
    \.html? $            # .htm or .html file ext at end of path

See also:

Embedded HTML

html`<a href="${url}">${text}</a>`

Shell commands

var proc = sh`ps ax | grep ${pid}`;

Byte strings

var buffer = bytes`455336465457210a`;