【问题标题】:Text editor: How can I duplicate second line to other code文本编辑器:如何将第二行复制到其他代码
【发布时间】:2017-10-04 14:38:27
【问题描述】:

<a target="_blank" href="img_forest.jpg">
  <img src="img_forest.jpg">

  <img src="img_forest110.jpg">

  <img src="img_forest228.jpg">

  <img src="img_forest334.jpg">

我需要复制行并更改 img src 到 target="_blank" 其他行的 href 并保持 img src 值相同。

有办法吗?

【问题讨论】:

  • 您使用什么编程语言来生成代码?还是您想使用 Javascript?
  • 我用记事本++
  • &lt;img target="_blank"替换&lt;img

标签: html notepad++ text-editor


【解决方案1】:
  • Ctrl+H
  • 查找内容:^\h*&lt;img src=("[^"]+")&gt;
  • 替换为:&lt;a target="_blank" href="$1"&gt;\n$0
  • 检查环绕
  • 检查正则表达式 请勿查看. matches newline
  • 全部替换

说明:

^           : begining of line
\h*         : 0 or more horizontal spaces (space or tabulation)
<img src=   : literally <img src=
(           : start group 1
  "[^"]+"   : 1 or more non quote character between quotes
)           : end group 1
>           : literally >

替换:

<a target="_blank" href="$1">   : <a...> tag, with value in group 1 (ie. image filename)
\n                              : linebreak (you could use "\r\n")
$0                              : content of group 0 (ie. the whole match: <img ...>)

给定示例的结果:

<a target="_blank" href="img_forest.jpg"">
  <img src="img_forest.jpg">

<a target="_blank" href="img_forest110.jpg"">
  <img src="img_forest110.jpg">

<a target="_blank" href="img_forest228.jpg"">
  <img src="img_forest228.jpg">

<a target="_blank" href="img_forest334.jpg"">
  <img src="img_forest334.jpg">

【讨论】:

    【解决方案2】:

    尝试使用正则表达式查找和替换:

    Find: <img src=\"(.*)\"> (you may add / at beginning and ending position if needed)
    Replace: <a target="_blank" href="$1"> (add / at beginning or ending if needed)
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-01-25
      • 2018-02-27
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多