I'm using Cheerio to get HTML from a local news website. When it returns the response, it keeps the escape literals (\n, \t
, etc.) in the string. Is there a way to use JS regex to replace all of these characters? I've tried using the replace()
function but it doesn't seem to replace all of them.
Example of the title of the news article I receive back (in json form): "\n\t\t\t\t\t\t\t\t\t
[news article title here]\t\t\t\t\t\t\t\t\t\t\t
:
Expected output: "[news article here]"
Thanks.
CodePudding user response:
Try using a regex with a global selector. See the g
in the following regex, which will get all instances of tabs or breaks replaced.
str.replace(/(\n|\t)/g, '');
Here is a stackblitz with this working.