ASP.net - 正则表达式添加属性
我需要使用正则表达式找到最佳解决方案,以查找 HTML 块内的 URL 并在标签内添加新属性 Name="true"。
下面是一个 HTML 示例(可以包含嵌入的 JS),我只需要将新属性 Name="true" 添加到任何 URL,并且不会影响任何嵌入的 JS 文件。
HTML 示例:
<HTML>
<a href="abc.aspx">
<a href="abc.aspx">
<a href="abc.aspx">
<script type="javascript">
function{
if("somefile.aspx")
{
do something...
}
}
</script>
</HTML>
预期 HTML:
<HTML>
<a href="abc.aspx" Name="true">
<a href="abc.aspx" Name="true">
<a href="abc.aspx" Name="true">
<script type="javascript">
function{
if("somefile.aspx")
{
do something...
}
}
</script>
</HTML>
I need to find the most optimal solution using Regex to find URL's inside a block of HTML and add a new attribute Name="true" inside the tag.
Below is an example of HTML (which can contain embedded JS), I just need to add the new attribute Name="true" to any URL and NOT effect any embedded JS file.
Example HTML :
<HTML>
<a href="abc.aspx">
<a href="abc.aspx">
<a href="abc.aspx">
<script type="javascript">
function{
if("somefile.aspx")
{
do something...
}
}
</script>
</HTML>
Expected HTML :
<HTML>
<a href="abc.aspx" Name="true">
<a href="abc.aspx" Name="true">
<a href="abc.aspx" Name="true">
<script type="javascript">
function{
if("somefile.aspx")
{
do something...
}
}
</script>
</HTML>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
/href="([_allowed_characters_in_URL_]+)"/
替换为'href="$1" Name="true"'
。Replace
/href="([_allowed_characters_in_URL_]+)"/
by'href="$1" Name="true"'
.