简体   繁体   中英

JS Regex to check that string only includes anchor

I am trying to create a regex that will return true only when the string only contains an anchor like so <a href="www.something.com">Link</a> .

Currently I have the following regex which almost works (?=.*^<a)(?=.*<\\/a>).*/g

This works for the following scenarios:

<a href="www.something.com">Link</a> - Matches successfully

words before <a href="www.something.com">Link</a> - No matches - success

<a href="www.something.com">Link</a> words after - finds match - Not successful :(

I think I'm pretty close, I just need to know how to not find a match if there are any characters after the </a> .

Add a $ after the last >

(?=.*^<a)(?=.*<\\/a>$).*

regex101

Alternatively, this regex matches links and discards everything else, irregardless of whether there are one or more links in the same line.

<a.*?\\/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