【问题标题】:Using Sublime Text find/replace to add attribute to an input tag使用 Sublime Text 查找/替换向输入标签添加属性
【发布时间】:2019-06-10 13:56:45
【问题描述】:

我有一个像这样的简单输入标签

<input type="password" name="repassword" placeholder="Enter password">

我想使用 Sublime Text 3 并添加一个 aria-label,其代码与 placeholder 中的代码相同。我想要的最终结果是这样的

<input type="password" name="repassword" placeholder="Enter password" aria-label="Enter password">

我把这个正则表达式放在 Search 字段 &lt;input.*?placeholder="(.*?)"

这在 Replace 中归档 &lt;input.*?placeholder=".*?" aria-label="$(1)"

但我最终得到了这个&lt;input.*?placeholder=".*?" aria-label="$(1)"&gt;

更新:我在 Sublime Text 中启用了 RedEx 按钮,这不是问题。因为搜索效果很好。

【问题讨论】:

    标签: sublimetext3


    【解决方案1】:

    这里有几个问题。

    字符序列.*?在替换文本中没有任何特殊含义,仅在原始搜索中。因此,将其包含在替换文本中只会将那些确切的字符放回去。如果您打算放回原来存在的相同文本,则需要 capture 它,就像您对 placeholder 文本所做的那样。

    其次,将捕获的文本插入回的语法是\1$1$(1) 也不特殊,只是直接注入那些字符。

    为了扩展您的原始示例,您希望捕获第一个 .*? 以及第二个,并更改替换文本以适应:

    查找: &lt;input(.*?)placeholder="(.*?)"
    替换: &lt;input$1placeholder="$2" aria-label="$2"

    请注意,第一个捕获编号为1,第二个编号为2,依此类推。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 2014-08-12
      • 2013-09-10
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多