繁体   English   中英

正则表达式:如何在Java中替换此字符串?

[英]Regex : how to replace this string in java?

所以我有这个java String

<script type="text/javascript"  
    src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type='text/x-mathjax-config'>
    MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>

我要替换为""

我在做什么是这样的:

.replaceAll("(<img[^>]*>)|(<script[^>]*>)", "")

但这不会删除MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']]}}); 脚本标签之间。

上面的正则表达式也查找所有<img>标记,并将它们替换为"" ,我想对上面显示的<script>标记以及它们之间的所有内容都实现同样的效果。

这似乎正在工作

str = Pattern.compile("<img .+?</img>|<script .+?</script>", Pattern.DOTALL).matcher(str).replaceAll("");

请注意,您的str有CRLF这就是为什么DOTALL

If you want to replace part of the string between the tags to replace then try this.

string = string.replaceAll("(<script>.*?)Here is our keyword which should be replaced(.*?</script>)","$1Replaced with:$2");

如果要用“”替换标签之间的所有内容,请尝试此操作。

s = s.replaceAll("(<script>).*?(</script>)","");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM