Reputation:
I have to setup a config file by code and pass in some variables
const confEntry = String.raw`
[${internal}]
...
user = bg${internal}
auth_user = bg${internal}
...
secret = ${password}
...
from_sip_src_setting=from_display,sip_from_user_setting=${account_username}
...
`;
when it comes to
from_sip_src_setting=from_display,sip_from_user_setting=${account_username}
I don't want to pass in a variable. =${account_username}
should be written as plain text. Obviously I get the error
account_username is not defined
How can I ignore this and write plain text for this specific part?
Upvotes: 2
Views: 2131
Reputation: 13963
If you want to keep ${}
in your final string, you can escape either the dollar sign, the curly braces or both using a backslash \
, this will break the ${}
pattern and it will be treated as regular text:
const world = 'world';
console.log(`\${hello} $\{world\} \$\{hello\} ${world}`);
However, since
String.raw
escapes everything, you cannot use that trick.
But, using the above trick, you could generate the ${str}
string using an inner template string like this:
const world = 'world';
const raw = x => `\${${x}}`;
console.log(String.raw`${raw('hello')} ${world}`);
Or simply:
const world = 'world';
const raw = x => '${' + x + '}';
console.log(String.raw`${raw('hello')} ${world}`);
Upvotes: 2
Reputation: 37755
String.raw used to get the raw string form of template strings, that is, substitutions (e.g. ${foo}) are processed, but escapes (e.g. \n) are not.
String.raw do not process your escapes.
let a = String.raw`hello\nhow are you`
let b = `hello\nhow are you`
console.log(a) //raw string output
console.log(b) // template string output
Upvotes: 0
Reputation: 9174
You will need to escape the curly braces in order for it not to be interpreted as a string literal
So instead of ${account_username}
ist would be $\{account_username\}
Upvotes: 1