记录一个查询兼容性的网站,String.replaceAll()兼容性报错

Can I use... Support tables for HTML5, CSS3, etc

这个网站是可以查询CSS、JS在浏览器各个版本中的兼容性

     今天遇到一个String.replaceAll()报错的问题,在我电脑上正常,换了一个电脑后就报错,一开始以为是代码问题,后来看见了一篇文章发现是浏览器版本导致的。

关于String.replaceAll()兼容性问题_小茄子Seven的博客-CSDN博客_replaceall兼容性

     replaceAll不支持一些低版本的浏览器,如果在开发中不能确保浏览器的版本,尽量使用其他的方案替代replaceAll

 解决方案

1. 利用splite()join()

let str = '2021.01.25'//要将其转换成2021-01-25
console.log(str.split('.').join('-'))//2021-01-25

2. replace() 

let str = '2021.01.25'//要将其转换成2021-01-25
console.log(str.replace(/\./g,'-'))//2021-01-25

   replace的兼容性比replaceAll好。replace()接受两个参数,第一个参数是需要替换的字符,支持正则,如果不增加正则的 关键字,则表示提换第一个匹配的字符串。

 

以下是使用Apache POI和DOM4J库将doc文档转换为XML文档并生成file文件的Java示例代码: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.AbstractWordUtils; import org.apache.poi.hwpf.converter.WordToHtmlConverter; import org.apache.poi.hwpf.usermodel.Range; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class DocToXmlConverter { public static void main(String[] args) { String docFilePath = "/path/to/doc/file.doc"; String xmlFilePath = "/path/to/xml/file.xml"; try { HWPFDocument doc = new HWPFDocument(new FileInputStream(docFilePath)); Range range = doc.getRange(); WordToHtmlConverter converter = new WordToHtmlConverter( DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); converter.processDocumentPart(doc, range); String html = converter.getDocument().asXML(); Document dom = DocumentHelper.parseText(html); Element root = dom.getRootElement(); replaceSpecialChars(root); File xmlFile = new File(xmlFilePath); FileOutputStream fos = new FileOutputStream(xmlFile); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(fos, format); writer.write(dom); writer.close(); System.out.println("XML file generated successfully at " + xmlFilePath); } catch (IOException | DocumentException | ParserConfigurationException e) { e.printStackTrace(); } } private static void replaceSpecialChars(Element element) { String text = element.getText(); if (text.contains("&")) { text = text.replaceAll("&", "&"); element.setText(text); } for (Element child : element.elements()) { replaceSpecialChars(child); } } } ``` 此代码将读取`docFilePath`指定的DOC文件,将其转换为HTML格式,然后将HTML解析为DOM对象。接下来,它将遍历DOM树,将所有的特殊符号(如"&")替换为其XML实体值(如"&")以保证兼容性。最后,将生成的XML文档写入`xmlFilePath`指定的文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值