Hone logo
Hone
Problems

Template String Parser in JavaScript

Template strings (also known as template literals) provide a powerful and convenient way to embed expressions inside strings in JavaScript. This challenge asks you to implement a simplified parser for template strings, allowing you to understand the underlying mechanics of this feature. Building this parser will enhance your understanding of string manipulation, regular expressions, and parsing techniques.

Problem Description

You are tasked with creating a JavaScript function called parseTemplateString that takes a template string as input and returns a new string with the embedded expressions evaluated and replaced with their corresponding values. The function should handle basic template string syntax, including ${expression}.

What needs to be achieved:

The function must parse the input template string, identify all expressions enclosed within ${...}, evaluate those expressions using eval(), and replace the entire ${expression} sequence with the result of the evaluation.

Key Requirements:

  • The function must correctly identify and extract expressions within ${...}.
  • The function must evaluate the extracted expressions using eval(). Note: While eval() is used here for simplicity in demonstrating the parsing logic, in a production environment, consider safer alternatives like new Function() or a dedicated expression evaluator to mitigate security risks.
  • The function must replace the original ${expression} sequence with the evaluated result.
  • The function should handle nested template strings correctly (i.e., expressions within expressions).
  • The function should handle empty expressions (e.g., ${}) correctly.

Expected Behavior:

The function should return a new string with all expressions evaluated and replaced. The original template string should not be modified.

Edge Cases to Consider:

  • Empty template strings.
  • Template strings with no expressions.
  • Template strings with only expressions.
  • Expressions containing special characters or operators.
  • Nested template strings.
  • Empty expressions (e.g., ${}).
  • Invalid expressions that would cause eval() to throw an error (handle gracefully - see Notes).

Examples

Example 1:

Input: "Hello, ${name}! You are ${age} years old."
Output: "Hello, John! You are 30 years old."
Explanation: The function replaces `${name}` with "John" and `${age}` with "30".  Assume `name` and `age` are globally defined variables.

Example 2:

Input: "The result is ${2 + 2}."
Output: "The result is 4."
Explanation: The function evaluates the expression "2 + 2" and replaces `${2 + 2}` with "4".

Example 3:

Input: "Value: ${}"
Output: "Value: undefined"
Explanation: An empty expression evaluates to `undefined`.

Example 4:

Input: "Outer: ${inner()}${1 + 1}"
Output: "Outer: Hello12"
Explanation: Demonstrates nested expressions and simple arithmetic. Assume `inner()` is a globally defined function that returns "Hello".

Constraints

  • The input string will be a valid JavaScript template string (enclosed in backticks ``).
  • The expressions within ${...} will be valid JavaScript expressions.
  • The function must handle strings up to 1000 characters in length.
  • The function should aim for reasonable performance; avoid excessively complex or inefficient algorithms.

Notes

  • Error Handling: If eval() throws an error (e.g., due to a syntax error in the expression), the function should catch the error and replace the entire ${expression} sequence with the string "ERROR". This prevents the entire program from crashing.
  • Global Scope: For simplicity, assume that all variables and functions referenced within the expressions are available in the global scope.
  • Security: As mentioned earlier, using eval() can be a security risk if the input template string comes from an untrusted source. In a production environment, consider using safer alternatives.
  • Regular Expression: You'll likely need to use regular expressions to identify and extract the expressions within the template string. Be mindful of escaping special characters within the regular expression.
  • Recursive Approach: Consider a recursive approach to handle nested template strings effectively.
  • String Immutability: Remember that strings in JavaScript are immutable. You'll need to build a new string to store the result.
Loading editor...
javascript