Function formatReplacementString

  • Formats a string, replacing {replacer key} with the value in the replacers at that replacer key (or multiple replacer values if there are multiple in the string). Will also remove \ before curly braces if curly braces are escaped with a backslash in order to preserve the curly braces. E.g. 'Hi, this is {name}! I like \{curly braces\}! would become Hi, this is Jim! I like {curly braces}!

    If the key in unescaped braces is not found, returns the key without the braces. Empty unescaped curly braces will just return a string without the braces e.g. ('I am {Nemo}', { 'name': 'Jim'}) would return 'I am Nemo'.

    Parameters

    • str: string

      String to format

    • replacers: object | { [key: string | number]: unknown }

      Object whose keys are replacer keys and whose values are the values with which to replace {replacer key}s found in the string to format. Will be coerced to strings using ${replacerValue}

    Returns string

    Formatted string

    formatReplacementString(
    'Hi, this is {name}! I like \{curly braces\}! I have a {carColor} car. My favorite food is {food}.',
    { name: 'Bill', carColor: 'blue' }
    );

    =>

    'Hi, this is Bill! I like {curly braces}! I have a blue car. My favorite food is food.'
MMNEPVFCICPMFPCPTTAAATR