9894

escape dollar sign in es6 string literal

Question:

I am using the new string literal syntax of javascript es6 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals" rel="nofollow">Docs Here</a> and I am not quite sure how to escape the dollar sign that is used to break the string to add a parameter. Here is what I am trying to do:

var response = `I consent to my credit card being charged in the amount of $ ${ total } for the purchase of ${ item.title } and any applicable sales tax.`

that works fine... but I would really prefer to not have that space $ ${title}

that leaves the end result looking like :

<blockquote>

... in the amount of $ 25.99 for the purchase...

</blockquote>

I would really rather prefer

<blockquote>

... in the amount of $25.99 for the purchase ...

</blockquote>

I guess that is ok, or obviously I could use the old way that still works, but it would be nice to know how to fix this. I linked to the Mozilla docs , and I can't find anything in there about it, hopefully someone has an idea how to fix this

Answer1:

var response = `I consent to my credit card being charged in the amount of $${ total } for the purchase of ${ item.title } and any applicable sales tax.`

Answer2:

The only case where $ does not produce the literal $ is before a {, otherwise you do not need to escape it.

var response = `You have $${money}`

does work therefore. In case you need to escape anything, the backslash \ is the escape character in template strings as well, so (while unnecessary) the following works as well:

var response = `You have \$${money}`

Answer3:

Putting a backslash in front of the dollar sign was the first thing that comes to my mind and it works:

\${DEPLOYMENT_NAME}

Answer4:

This works for me.

var response = `You have \$\{money}`;

Recommend

  • Function annotations
  • ReadToEnd from std output of process and waitforexit
  • Group and count events per time intervals, plus running total
  • NoClassDefFoundError in Android Plugin for Unity
  • Difference between Tuple and List[Any] in Scala?
  • how to display a image over a map with imshow?
  • Do python objects move in memory during execution?
  • Efficient design for a auto refresh mobile app
  • Error in nested RibbonApplicationMenuItem
  • Ember.js: How to integration test components that interact with ember-data models
  • AngularJS directive link function not called in jasmine test
  • How to clone an object in Flex?
  • Evaluate at which size data.table is faster than data.frame
  • “a” Does not contain a definition for“b” and no extension method ' b ' accepting a first a
  • Is it possible to specify 16 colums to bootstrap 3
  • php list into a table
  • Render Box Dynamically in Shiny
  • Grails Acegi manual login
  • Distributed Cache feature in YARN
  • Catch SqlException when Attempting NHibernate Transaction
  • How to pass values to a Custom Resolver in Automapper?
  • Load more status from database
  • Use office 365 login/credentials as Single Sign On
  • Why context.Wait in StartAsync didn't stop the dialog
  • SFINAE for class member function (one compiles the other not)
  • Is there way to structure a QueryExpression so that you could dynamically handle a unknown number of
  • Button On Click event not firing
  • How to add tag during runtime in C#
  • getting the values of checkboxes in a checkboxlist control
  • Unable to start a WebView from an AsyncTask
  • view details for exception in vs 2017
  • C# code can't “see” the methods in my C++ dll
  • All Event listing on specified date in Google Calender api (V3) in java?
  • How to run chrome.tabs.insertCSS from the background page on each page?
  • How to configure the APNS.Certificate in the arm template
  • Access to a Matlab gui from the web