Skip to content

Support for variable interpolation inside template strings #72

@trezy

Description

@trezy

Copied from this Reddit conversation

I'd love to see support for interpolation of template literals in the compiled RegEx from the Babel plugin. For example, I can almost accomplish this with the Babel compiler using Melody's raw method:

// Original
new RegExp(/*melody*/ `
  "foo"; 
  \`\${bar}\`;
  "baz";
`)

// Compiled
new RegExp("foo${bar}baz")

It seems that this could be fixed just by wrapping the string output in backticks instead of quotes. I originally assumed this would have unintended consequences, but since $, {, and } are all special RegEx characters, they're automatically escaped in string literals. This protects us from misinterpreted literals:

// Original
new RegExp(/*melody*/ `
  "foo";
  "${bar}";
  "baz";
`)

// Compiled
new RegExp("foo\$\{bar\}baz")

I did come up with this while sleepy, sp=o it's entirely possible that I may be missing something. 🤔

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions