如何在Google Tag Manager中移除JS返回结果里的双引号(\x22)
Solution to Remove Double Quotes from H1 Text
Got it, let's fix this up for you! Your current code successfully grabs the H1 text, but when there are double quotes in the content, you want to strip them entirely instead of dealing with escaped characters. Here's the adjusted code:
function() { var name = document.querySelector('h1').textContent.replace(/"/g, ''); return name; }
How this works:
- We add the
replace()method to the text content of your H1 element. - The regex
/"/gtargets all double quotes in the text (thegflag stands for "global"—it ensures we don't stop at the first quote we find). - Replacing each matched quote with an empty string (
'') removes every double quote from the final result.
For your example <h1>5" Boning Knife</h1>, this modified code will return 5 Boning Knife exactly as you need.
If you ever wanted to replace quotes with something else (like a space or a different character), you just swap the empty string for your desired replacement value.
内容的提问来源于stack exchange,提问作者chappers
相关产品推荐
相关产品推荐

