简体   繁体   中英

JavaScript and XML and HTML tags

Using JavaScript to create an xml file (for later saving in PDF). When user enters some HTML characters, like < and >, these cause problems because the program thinks they are Beginning and End HTML tags. We've tried the Replace function but have not found the correct Syntax yet. Any ideas?

You might try :

'<foo><bar></foo></bar>'.replace(/>/g, '&lt;').replace(/</g, '&gt;')

The g at the end of the regex is really important b/c otherwise it will only replace the first occurence.

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