简体   繁体   中英

How to replace escape characters in a string JavaScript

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM