regex replace urls with anchors ONLY if not already in anchors

后端 未结 3 1316
予麋鹿
予麋鹿 2021-01-21 22:54

I\'ve seen similar questions asked before, but none with a working solution.

I am trying to replace all urls on a page with anchor tags, but only those which aren\'t alr

3条回答
  •  一生所求
    2021-01-21 23:16

    For jobs like this, I normally recommend people do it with code rather than regex because regex gets really messy, really fast. However, if you do want a regex, here is a workable solution. Please go to the link to get a full understanding and view of test cases I used.

    http://regex101.com/r/kL3iL7

    (?:http([s]?):\/\/)?((\w+[.])+\w+(\/\w*)*(\?[^\s]*)*)(?![^\s]*>)
    

    with replacement

    \2
    

    I do not promise that is is perfect, but it does handle a lot of cases. Let me know if there are any test cases it needs to be fixed for.

提交回复
热议问题