java 用replace去除href及后面的字符串
要使用replace()方法去除href及后面的字符串,你可以使用正则表达式来匹配并替换。
下面是示例代码:
String input = "<a href='https://www.example.com'>Link</a>";
String output = input.replaceAll("href='[^']*'", "");
System.out.println(output);
输出结果将会是:
<a>Link</a>
在上面的代码中,replaceAll()方法接受一个正则表达式作为参数,并用空字符串进行替换。正则表达式href='[^']*'用于匹配href属性及其值,[^']*表示匹配任意不是单引号的字符。这样,href及其值会被替换为空字符串,达到去除的效果。
原文地址: https://www.cveoy.top/t/topic/ikVV 著作权归作者所有。请勿转载和采集!