简体   繁体   中英

How can I save text with formatting in JavaScript?

I'm working on a Chrome Extension that has a notes page as a feature. It's not the main feature (meaning it won't be a lot of text) so I don't need to store the notes in a database, so I plan on storing the notes in LocalStorage.

Is there a way I can store this text with formatting such as new lines. For example, I want the following text:

Hello,

world.

to be saved as is, instead of "Hello,world."

To format the text, you need to use:

    const formattedText = text.split('\n').join("\\n")

To get the text back, you should use:

    const text = formattedText.split('\\n').join('\n')

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