Regex ignore URL already in HTML tags

前端 未结 1 1440
無奈伤痛
無奈伤痛 2020-12-04 02:34

I\'m having a little problem with my Regex

I\'ve made a custom BBcode for my website, however I also want URLs to be parsed too.

I\'m using preg_replace and

相关标签:
1条回答
  • 2020-12-04 03:03

    Try this

    (?<!href=")(\b[\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])
    

    See it here on Regexr

    To make it more general you can simplify your lookbehind to check only for "=""

    (?<!=")(\b[\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])
    

    See it on Regexr

    (?<!href=") is a negative lookbehind assertion, it ensures that there is no "href="" before your pattern.

    \b is a word boundary that anchors the start of your link to a change from a non word to a word character. without this the lookbehind would be useless and it would match from the "ttp://..." on.

    0 讨论(0)
提交回复
热议问题