我有一個用例,我想在html字符串中替換一些值,所以我需要爲replaceAll做這件事,但那不工作,雖然替換工作正常,這裏是我的代碼:模式替換字符串中的轉義字符失敗與replaceAll
String str = "<style type=\"text/css\">#include(\"Invoice_Service_Tax.css\")</style>";
String pattern = "#include(\"Invoice_Service_Tax.css\")";
System.out.println(str.replace(pattern, "some-value"));
System.out.println(str.replaceAll(pattern, "some-value"));
輸出爲:
<style type="text/css">some-value</style>
<style type="text/css">#include("Invoice_Service_Tax.css")</style>
對於我的使用情況下,我只需要做的replaceAll,我試着用下面的模式,但也沒有幫助:
"#include(\\\"Invoice_Service_Tax.css\\\")"
"#include(Invoice_Service_Tax.css)"
這個工作對我來說:) – user2098324