简体   繁体   English

如果href值为正则表达式,则将锚标记值替换为锚标题

[英]Replace anchor tag value with anchor title if href value is empty by regex

I am trying to replace the anchor tag value with anchor's title value if any anchor tag's href attribute is blank. 如果任何锚标记的href属性为空,我试图用锚的标题值替换锚标记值。

Like 喜欢

<a xlink:href="">Lorem Ipsum</a>  is simply dummy text of the printing and typesetting industry<a xlink:href ="http://google.com">Google</a>. 

if I try with below regex: 如果我尝试以下正则表达式:

<a [^>]+>(.*?)<\/a>

DEMO http://regexr.com/3h6on 演示http://regexr.com/3h6on

then output I am getting below 然后输出我正在下面

Lorem Ipsum is simply dummy text of the printing and typesetting industry Google. 

it replaced both anchor tag with Anchor Text value, and I need to replace those anchor tag where the href attribute value is empty. 它用Anchor Text值替换了两个锚标记,并且我需要替换href属性值为空的那些锚标记。

I need an output like below 我需要如下输出

Lorem Ipsum is simply dummy text of the printing and typesetting industry<a xlink:href ="http://google.com">Google</a> 

My approach looks rather messy but for the test link you provided it seems to work just like you want it to: 我的方法看起来很凌乱,但是对于您提供的测试链接,它似乎可以按您希望的那样工作:

<a [^>]+?(?=href=(?:"|')(?:"|'))[^>]*?>(.*?)<\/a>

Try it with this link. 尝试使用此链接。


What I've added to the regex is the following part 我添加到正则表达式的是以下部分

[^>]+?(?=href=(?:"|')(?:"|'))[^>]*?

The . 的。 wildcards at the beginning and end of the part I've added are just there to match with any additional attributes or whitespaces between the < >. 我添加的部分开头和结尾的通配符就可以与<>之间的任何其他属性或空格匹配。

The regex inside the parenthesis is called a positive lookahead . 括号内的正则表达式称为正向超前 This means that it will match whatever is inside the parenthesis but will not include it into the results. 这意味着它将匹配括号内的任何内容,但不会将其包括在结果中。

The positive lookahead matches an href followed by an empty string. 正向超前匹配一个href,后跟一个空字符串。

Note that my regex still matches if the quotation marks after the href= expression do not match 请注意,如果href =表达式后的引号不匹配,我的正则表达式仍然匹配

I hope this answered your question. 我希望这能回答您的问题。

Please try this: <a[^<]+href=(['"]{2})\\1?[^>]*?>([^<>]*)<\\/a> 请尝试以下操作: <a[^<]+href=(['"]{2})\\1?[^>]*?>([^<>]*)<\\/a>

Try it here . 在这里尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM