Đặng Hải Triều
Đặng Hải Triều

Reputation: 33

Remove the dots in the string

I have a code

<script>
  !function(f,b,e,v,n,t,s)
  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
  n.callMethod.apply(n,arguments):n.queue.push(arguments)};
  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
  n.queue=[];t=b.createElement(e);t.async=!1;
  t.src=v;s=b.getElementsByTagName(e)[0];
  s.parentNode.insertBefore(t,s)}(window, document,'script',
  'https://connect.facebook.net/en_US/fbevents.js');
  fbq('init', '165468367444379213');
  fbq('track', 'PageView');
  fbq('track', 'Purchase', {
    value: '1.000.000',
    currency: 'VND',
  });  
</script>

How do I change the value of value: '1,000,000' to value: '1000000'

Thanks for the help :)

Upvotes: 1

Views: 151

Answers (3)

Gtm
Gtm

Reputation: 263

var mystring = 'okay.this.is.a.string';
var myNewString = escapeHtml(mystring);

function escapeHtml(text) {
if('' !== text) {
    return text.replace(/&amp;/g, "&")
               .replace(/&lt;/g, "<")
               .replace(/&gt;/g, ">")
               .replace(/\./g,' ')
               .replace(/&quot;/g, '"')
               .replace(/&#39/g, "'");
} 

Upvotes: 0

Mamun
Mamun

Reputation: 68933

Is the comma (,) or the dot (.) you want to remove?

You can use replace() with RegEx to replace all occurrences of dot (.) with empty string:

value: '1.000.000'.replace(/\./g,'')

OR:

value: '1.000.000'.replace(/[.]/g,'')

var obj = {
  value: '1.000.000'.replace(/\./g,''),
  currency: 'VND',
}
console.log(obj.value);

Your code should be:

!function(f,b,e,v,n,t,s)
  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
  n.callMethod.apply(n,arguments):n.queue.push(arguments)};
  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
  n.queue=[];t=b.createElement(e);t.async=!1;
  t.src=v;s=b.getElementsByTagName(e)[0];
  s.parentNode.insertBefore(t,s)}(window, document,'script',
  'https://connect.facebook.net/en_US/fbevents.js');
  fbq('init', '165468367444379213');
  fbq('track', 'PageView');
  fbq('track', 'Purchase', {
    value: '1.000.000'.replace(/\./g,''),
    currency: 'VND',
  });  

Upvotes: 1

Luay
Luay

Reputation: 814

Something like this with replace() would do. Recall that by adding /,/g instead of "," without the g only effects the first instance encountered of the period.

var text = document.getElementById("text").innerHTML;

console.log(text.replace(/,/g,""));
<div id="text">1,000,000</div>

Upvotes: 0

Related Questions