简体   繁体   中英

Node.js string escape characters translation

在此输入图像描述

The first case, I have use the fs.readFileSync() to read html content from the local file, and I call the console.log() ,it output normally with format.

The second case,I use the regex to match the content from a text(not file), and it output with some escape characters.

For the second case, how can I translate the output just like the first case?Remove all the escape characters with format.

Replace the escape characters using the Javascript replace method String.replace()

var newString = $3.replace(/\\n|\\/g,"");
console.log(newString);

Generally speaking, you can parse special charachters by putting an extra "\\" before them. For example if you wanted to refer to "\\n" in a string literally, you can use "\\n". To replace any of the special characters in a string, do;

var newString = $3.replace(/\\n|\\/g,"string-you-want-to-replace-with");
console.log(newString);

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