Search code examples
javascriptschemagoogle-tag-managerjson-ld

How to fix "Bad escape sequence in string" in schema description


I have use GTM to tag the schema variables.

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Product",
  "name": "{{Product-name}}",
  "description": "{{Product-description}}",
  "category": "{{Product-category}}",
  "image": "{{Product-image}}",
  "sku": "{{Product-reference}}",
  "offers": {
    "@type": "Offer",
    "url": "{{Page Path}}",
    "Price": "{{Product-price-with-dot}}",
    "priceCurrency": "[EUR]",
    "itemCondition": "https://schema.org/NewCondition",
    "availability": "{{Product-stock}}",
    "seller": {
      "@type": "Corporation",
      "name": "Company Name"
    }
 }
}
</script>

All seems OK except for the variable value "Description" of some products whose description contains special characters. The original text is converted with more and more special caracteres... and return "Bad escape sequence in string".

Product-description variable set up

Original text description :

id="pPdtDesc">Shopping bag.

Product description with "Bad escape sequence in string" result

How can I fix all any possible problems with special characters?


Solution

  • The answers was :

    function(){
    var string = {{Product-description}};
    return string.replace(/[^a-zA-Z0-9]/g, '');
    }