简体   繁体   中英

Javascript regex: remove all attributes except for href from multiple anchor tags. Can not include double quotes in regex

So I have to strip out all attributes from multiple anchor tags, except for the href="some value". The templating engine that I am using allows for server-side functions to run before rendering the final result, but for some internal reason I am unable to include a double quote in the regex otherwise the function just fails silently. So with that being said let us say that I have the following HTML:

<p>just a bunch of text here<a data-sv-linklookup-id="https://www.somesite.com/somevalue/?i=632738&amp;ver=html5" data-sv-linklookup-type="plugins_nav_external_link" href="https://www.somesite.com/somevalue/?i=632738&amp;ver=html5" target="_blank">view it online</a> or request it through our<a data-sv-linklookup-id="5a8dad3e2f124e053ecfe720" data-sv-linklookup-type="plugins_nav_navitem_primary_main" href="https://www.somesite.com/plan-your-trip/free-visitor-guide/" target="_self" title="some title">online form</a>. a lot more text here<a data-sv-linklookup-id="5a8dad402f124e053ecfebd2" data-sv-linklookup-type="plugins_nav_navitem_primary_main" href="https://www.somesite.com/" target="_self" title="some title">some more text</a></p>

So far I have tried the following:

/data-sv-linklookup-id=.[^\s]*|data-sv-linklookup-type=.[^\s]*|target=.[^>]*|title=.[^>]*/g

Which results in:

<p>just a bunch of text here<a   href="https://www.somesite.com/somevalue/?i=632738&amp;ver=html5" >view it online</a> or request it through our<a   href="https://www.somesite.com/plan-your-trip/free-visitor-guide/" >online form</a>. a lot more text here<a   href="https://www.somesite.com/" >some more text</a></p>

This works just fine for my purposes, but there is a potential that there may by other attributes added and I just can't really add all of the possibilities to the conditional. Thanks for any input

Edit: does not include double quotes, uses ascii code instead.

https://regex101.com/r/KNMcZh/5

Then to replace do:

yourString.replace(/<a.*?(href=\x22.*?\x22).*?>/g, '<a $1>');

Note that will only work for anchors that contain an href. To all the attributes from anchors that do not contain an a href do:

yourString.replace(/<a .*?>/g, '<a>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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