【问题标题】:How to replace <span style="text-decoration: underline;"> </span> with <u></u> tag for a dynamic string in angular 6如何用 <u></u> 标记替换 <span style="text-decoration: underline;"> </span> 以获取角度 6 中的动态字符串
【发布时间】:2020-07-07 11:31:59
【问题描述】:

我有一个输入框,用户可以在其中输入字符串并对选定文本进行一些字符串格式设置,如字体名称、粗体、斜体、下划线、字体大小等。当用户在任何单词或句子下划线时,该单词/句子源代码就像&lt;span style="text-decoration: underline;"&gt;&lt;/span&gt; 如果用户更改字体大小或字体名称,则 &lt;span style="font-family"...&gt;&lt;span style="font-size"... 附上这些标签。

在此我必须将&lt;span style="text-decoration: underline;"&gt; tag 及其对应的&lt;/span&gt; 替换为&lt;u&gt;and &lt;/u&gt;。怎么做。因为替换&lt;span style="text-decoration: underline;"&gt; 标签很容易。我可以找到并更换它。但是如果有多个&lt;/span&gt; 标签,替换它的&lt;/span&gt; 标签是很困难的。因为我需要从多个&lt;/span&gt;标签中找出需要替换哪个&lt;/span&gt;标签。

我怎样才能在 Angular 6 中做到这一点。谢谢。 任何帮助将不胜感激。

【问题讨论】:

标签: html angular


【解决方案1】:

在 Javascript 中,您可以使用正则表达式进行这种替换:

var x = '<span style="text-decoration: underline;">Test</span>'

function replace_span_to_u(input) {
  return input.replace(/\<span style\=\"text-decoration\: underline\;\"\>(.*?)\<\/span\>/g,             function(matched, index) {
          return '<u>' + index + '</u>';
        })
  }

replace_span_to_u(x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-19
    • 2015-03-05
    • 2023-03-10
    • 1970-01-01
    • 2021-08-01
    • 2016-05-09
    • 2020-05-27
    • 1970-01-01
    相关资源
    最近更新 更多