所以我的公司正在为他们的移动网站使用第三方,我们有一个控制台来更新一些代码并通过它们控制内容.其中一项是搜索和替换功能,可以更新站点的代码.唯一的一点是,它使用了很多复杂的正则表达式代码,我似乎无法找到关于复杂内容的好教程.所以这是他给我的例子,棍子抓住段落标签并把它放在链接中
搜索
(#d6d6d4.+?>.+?<p><a.+?>.+?)</a>(.+?)</td>
Run Code Online (Sandbox Code Playgroud)
用...来代替
$1$2</a></td>
Run Code Online (Sandbox Code Playgroud)
什么是$ 1和$ 2代表什么?我知道它可能与其中一个有关.+?但我不确定哪一个.如果有人知道请帮助我.我在下面的代码中添加了正则表达式变量旁边的数字
(#d6d6d4.+?**[1]**>.+?**[2]**<p><a.+?**[3]**>.+?**[4]**)</a>(.+?**[5]**)</td>
Run Code Online (Sandbox Code Playgroud)
ala*_*lan 20
$ 1和$ 2代表正则表达式中捕获组中的文本.捕获组是括号内的内容.
( // start first capture group
#d6d6d4 // match #d6d6d4
.+?> // any character, non-greedy, up to '>'
.+?<p> // any character, non-greedy, up to <p>
<a.+?> // an <a..> tag, consuming everything up to '>'
.+? // all characters from <a> to </a>
) // close the first capture group before the '</a>'
</a> // literal '</a>'
( // start second capture group
.+? // match all, non-greedy up to '</td>'
) // close capture group before '</td>'
</td> // literal '</td>'
Run Code Online (Sandbox Code Playgroud)
所以,如果你有这个字符串: <td color=#d6d6d4 foo=bar>Hello, world<p><a href=http://foo.com>foo link</a>some more text</td>
$ 1场比赛:#d6d6d4 foo=bar>Hello, world<p><a href=http://foo.com>foo link
$ 2场比赛:some more text
所以字符串转换为:
<td color=#d6d6d4 foo=bar>Hello, world<p><a href=http://foo.com>foo linksome more text</a></td>
这基本上意味着</a>标签在之后some more text(或者在</td>您喜欢之前)之前移动