Regex从除<link>标签
Regex to get the link in href from the tags other then <link> tag
我在一个javascript变量中有一段文本,其中包含多个锚点、区域和链接标记。我想用其他链接替换除链接标签之外的所有href链接。例如,我当前的正则表达式匹配所有不包含(mailto:)和(abc-url)的href
var r_domain = 'testlink.com';
var s = 'someencryptedstring';
var pattern = /href['s]*=['s]*('|")(?!mailto:)(?!#)((?:(?!'abc-url'b)[^('|")])*)('|")/ig;
var replace_pattern = 'href='"http://'+r_domain+'/link.php?str='+s+'&mailin-url=$2"';
var body = '<a href="http://example.com" >abc</a> test data <a href="http://test.com/test.php?str=someencryptedstring&abc-url=http://cdf.com" > link </a> test last <link rel="stylesheet" href="http://csslink.com/forms.css" type="text/css" media="screen, projection" /> area test <a href="http://example_1.com" > xyz </a>';
var re = new RegExp(pattern);
var replaced = body.replace(re , replace_pattern);
console.log(replaced);
它应该只替换以下链接:
href="http://example.com"
href="http://example_1.com"
它不应该取代以下链接:
href="http://test.com/test.php?str=someencryptedstring&abc-url=http://cdf.com"
href="http://csslink.com/forms.css"
输出应如下(console.log(已替换);):
<a href="http://testlink.com/link.php?str=someencryptedstring&mailin-url=http://example.com" >abc</a> test data <a href="http://test.com/test.php?str=someencryptedstring&abc-url=http://cdf.com" > link </a> test last <link rel="stylesheet" href="http://csslink.com/forms.css" type="text/css" media="screen, projection" /> area test <a href="http://testlink.com/link.php?str=someencryptedstring&mailin-url=http://example_1.com" > xyz </a>
我已经更改了模式和replace_pattern的正则表达式,并保持了其他脚本的原样。它对我来说很好。
模式和replace_pattern的新正则表达式如下:
var pattern = /(<(a|area) [^>]*)href['s]*=['s]*('|")(?!mailto:)(?!#)((?:(?!'abc-url'b)[^('|")])*)('|")/ig;
var replace_pattern = '$1href='"http://'+r_domain+'/link.php?str='+s+'&mailin-url=$4"';
相关文章:
- 在<页眉>标签
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- </脚本>标签放错地方了
- Jquery在点击<a>标签
- Ionic V1.3-标签-带条件的徽章样式<离子选项卡>
- <选择>标签不起作用
- 如何在<嵌入>标签在Plone 4.1中
- 在<script src=“"></脚本>标签
- 使用几个<脚本>标签不会't工作-只调用一个脚本
- 如何在更新面板内部调用/触发javascript函数<触发器>标签
- react-让一个元素返回两个相邻的<tr>标签
- <h1>标签在离子含量元素中不可见
- VS代码:在<脚本>标签
- 在</表单>标签
- Meteor.js:<脚本>标签不't在<身体>
- 替换<标签>,包含的表单控件除外
- 使用jQuery加载基于<标签>目录
- 可以't在<标签>标签
- 更改只有<标签>标记在<李>
- 点击<标签>在Mobile Safari中