var strObj = '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n{"text": true, ["text", "text", "text", "text"], [{ "text", "text" }]}\n\n\n'
I am trying to sanitize a string by stripping out the \n
but when I do .replace(/\\\n/g, '')
it doesn't seem to catch it. I also Google searched and found:
..in accordance with JavaScript regular expression syntax you need two backslash characters in your regular expression literals such as
/\\/
or/\\/g
.
But even when I test the expression just to catch backslash, it returns false:
(/\\\/g).test(strObj)
RegEx tester captures \n
correctly: http://regexr.com/3d3pe
Should just be
.replace(/\n/g, '')
unless the string is actually
'\\n\\n\\n...
that it would be
.replace(/\\n/g, '')
No need of using RegEx here, use String#trim
to remove leading and trailing spaces.
var strObj = '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n{"text": true, ["text", "text", "text", "text"], [{ "text", "text" }]}\n\n\n';
var trimmedStr = strObj.trim();
console.log('Before', strObj);
console.log('--------------------------------');
console.log('After', trimmedStr);
document.body.innerHTML = trimmedStr;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With