正则表达式用开始和结束替换多次出现的内容(Regular expression to replace multiple occurrence of content with start and end

编程入门 行业动态 更新时间:2024-10-26 00:22:06
正则表达式用开始和结束替换多次出现的内容(Regular expression to replace multiple occurrence of content with start and end)

我有一个像下面的字符串

Mickey <script> 333</script> 1 <script>eee</script> 2 <script>edddde</script> 3

我希望它像下面一样(将所有的开始脚本和结束脚本标记替换为空白)

米奇1 2 3

当我在下面尝试时(它将替换第一个和最后一个脚本标记之间的整个内容

data = data.replace(/<script>.*<\/script>/,"")

我得到米奇3

I have a string like below

Mickey <script> 333</script> 1 <script>eee</script> 2 <script>edddde</script> 3

and I want it to be like below (replace all occourrence of start script and end script tag with blank)

Mickey 1 2 3

When I try below (it replaces whole content between first and last script tag

data = data.replace(/<script>.*<\/script>/,"")

I get Mickey 3

最满意答案

使用非贪婪的操作符:

data = data.replace(/<script>.*?<\/script>/g, "");

Use the non-greedy operator:

data = data.replace(/<script>.*?<\/script>/g, "");

更多推荐

本文发布于:2023-08-05 00:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结束   内容   正则表达式   Regular   expression

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!