【问题标题】:Regex to make self closing tag正则表达式制作自闭标签
【发布时间】:2015-05-07 16:49:52
【问题描述】:

是否有任何正则表达式可以将 xhtml 标签转换为自闭合标签。例如

转换
<input type="text" id="textbox1">

<input type="text" id="textbox1"/>

【问题讨论】:

  • 正则表达式(&lt;[^&gt;]*)&gt;替换字符串$1/&gt;
  • 上面的正则表达式对我很有效。

标签: jquery html regex xhtml


【解决方案1】:

试试

var str = '<input type="text" id="textbox1">';
str = str.slice(0, str.lastIndexOf(">")).concat("/>");
console.log(str);

【讨论】:

  • 尝试制作适用于 HTML selft 结束标签的正则表达式。 有可能出现在
    在这种情况下,我该如何使用上述答案跨度>
  • var str = $("div input").get(0).outerHTML; str = str.slice(0, str.lastIndexOf("&gt;")).concat("/&gt;"); $("div").html(str) ?虽然浏览器可能会关闭 /
  • 什么是应用程序?
  • 申请是制作网页模板。它必须在页面拖动时添加控件,设置属性。并以 json 格式存储数据意味着以 json 格式保存 HTML 页面。我使用 xhtml 作为文档类型,浏览器从我不想做的所有自结束标记中删除 /。所有标签都应该关闭标签,以便我可以保存正确的 json 格式字符串
【解决方案2】:

如果没有自闭标签:

const s = `<input type="text" id="textbox1">`
console.log(s.replace(/<([^>]*)>/g, '<$1/>'))

解释

--------------------------------------------------------------------------------
  <                        '<'
--------------------------------------------------------------------------------
  (                        group and capture to $1:
--------------------------------------------------------------------------------
    [^>]*                    any character except: '>' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of $1
--------------------------------------------------------------------------------
  >                        '>'

如果已经有一些自闭合标签:

const s = `<input type="text" id="textbox1"> and <input type="text" id="textbox2"/>`
console.log(s.replace(/<([^>]*[^>\/\s])(?:\s*\/)?\s*>/g, '<$1/>'))

解释

--------------------------------------------------------------------------------
  <                        '<'
--------------------------------------------------------------------------------
  (                        group and capture to $1:
--------------------------------------------------------------------------------
    [^>]*                    any character except: '>' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    [^>\/\s]                 any character except: '>', '\/',
                             whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
  )                        end of $1
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  >                        '>'

【讨论】:

    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多