在下面的代码中,我尝试将text
fromString
次toString
替换为public static void main(String[] args) {
String fromString = "aaa(bbb)";
String toString = "X";
String text = "aaa(bbb) aaa(bbb)";
String resultString = text.replaceAll(fromString, toString);
System.out.println(resultString);
}
,但不进行替换。如何在正则表达式中设置括号以使其工作?
$script .= "$(document).ready(function() {";
$script .= "$('img.thumbnail').click(function() {";
$script .= "window.location.href='".$href."'.replace(/__selected_service__/, selected_service);";
$script .= "});";
$script .= "});";
$s .= '<input type="radio" name="app_select_services" id="'.$service->ID.'" value="'.$service->ID.'"'.$sel.' /><label for="'.$service->ID.'"><img class="thumbnail" src="http://kerrymotorservices.co.uk/wp-content/uploads/2015/04/'.$service->ID.'.png" title="'.$service_description.'"></label>';
答案 0 :(得分:4)
replaceAll
使用正则表达式作为其第一个参数。括号()
用于capturing groups,因此需要转义
String fromString = "aaa\\(bbb\\)";
由于you can't modify输入字符串,您可以使用Pattern.quote
String resultString = text.replaceAll(Pattern.quote(fromString), toString);
或者只是String.replace
可以使用不使用正则表达式参数
String resultString = text.replace(fromString, toString);