【问题标题】:Replace quotes of HTML tags attributes in a HTML string替换 HTML 字符串中 HTML 标记属性的引号
【发布时间】:2019-09-15 05:35:39
【问题描述】:

例如,我想用 javascript 正则表达式替换 HTML 标记中的每个 "'

<div class="test1">Content 1</div> <div class="test2">"Content ""2</div> 

<div class='test1'>Content 1</div> <div class='test2'>"Content ""2</div>

使用 C#,我需要在标签中替换 ', like;

<div class=\"test1\">Content 1</div> <div class=\"test2\">"Content ""2</div> 

<div class='test1'>Content 1</div> <div class='test2'>"Content ""2</div>

【问题讨论】:

  • 你试过了吗?你的问题到底是什么?
  • &lt;div class=\"test1\"&gt; 此标记中没有属性值。如果标签是&lt;div class="test1\"&gt;,就会有为了解析标签的属性值,它们必须是有效的,你不能随便编造。
  • 为什么要这样做?也许我们可以找到比您要求的更好的解决方案。
  • 我会想到&lt; 字符,然后是not &lt; or ' 字符一次或多次,然后是" 等。如果您尝试使用正则表达式并给我们一个实际问题,那就更好了
  • 另外,它不会帮助你阅读"Content ""2,因为这个="test2"&gt;"Content ""2

标签: javascript c# regex


【解决方案1】:

对于 C# 你可能会有一些想法。

  (?<=\=)"|"(?=\>)

Match 1
1.  "
Match 2
1.  "
Match 3
1.  "
Match 4
1.  "

演示::https://rubular.com/r/7NdNLySmZKlYnt

对于 Javascript:

演示:https://www.w3schools.com/code/tryit.asp?filename=G3G2FGAV1SGF

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2020-09-09
    相关资源
    最近更新 更多