字符串里面可能会有a标签,想要只替换没有a标签包裹的url,如果url被a标签包裹则不做处理。
场景是:
取出一段html为字符串
<p>< a href="https://www.segmentfault.com" target="_self"></ a>< a href="https://www.segmentfault.com">www.segmentfault.com</ a></p><p>www.segmentfault.com</p>
将这里面的url识别出来并包裹上a标签,但是href中的url不能变、已有a标签包裹的url不能变。
想要的结果是:
<p>< a href="https://www.segmentfault.com" target="_self"></ a>< a href="https://www.segmentfault.com">www.segmentfault.com</ a></p><p><a href="https://www.segmentfault.com">www.segmentfault.com</a></p>