尝试使用正则表达式替换html标记 [英] Trying to replace html tags using regex

查看:138
本文介绍了尝试使用正则表达式替换html标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我正在尝试替换

<script type='text/javascript'>some stuff</script>

with:

<div type='text/javascript'>some stuff</div>

我目前正在测试:

alert( o.replace( /(?:<\s*\/?\s*)(script)(?:\s*([^>]*)?\s*>)/gi ,'div') );

但我得到的是:

divsomestuffdiv

我怎样才能让它只替换script部分并保留其他标记和属性字符?

How can I get this to only replace the "script" portion and preserve the other markup and attribute characters?

推荐答案

您保留了开始和结束标记括号。所以试试这个:

You have keep the opening and closing tag brackets. So try this:

o.replace(/(<\s*\/?\s*)script(\s*([^>]*)?\s*>)/gi ,'$1div$2')

这篇关于尝试使用正则表达式替换html标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆