<?PHP $str = "word <a href=\"word\">word</word>word word"; $str = preg_replace("/word(?!([^<]+)?>)/i","repl",$str); echo $str; # repl <word word="word">repl</word> ?>
来源:http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/
不幸的是,我的项目需要一个仅适用于Java的语义库…
//谢谢Celso
解决方法
使用String.replaceAll()方法:
class Test { public static void main(String[] args) { String str = "word <a href=\"word\">word</word>word word"; str = str.replaceAll("word(?!([^<]+)?>)","repl"); System.out.println(str); } }
希望这可以帮助.