Replace non-html links with tags

前端 未结 1 874
再見小時候
再見小時候 2021-01-23 20:15

I have a block of code that will take a block of text like the following:

Sample text sample text http://www.google.com sample text

相关标签:
1条回答
  • 2021-01-23 20:50

    Add code to the beginning of the pattern to capture an opening anchor tag, and then do not perform the callback code when it has captured something:

    /(<a[^>]*>)?http:\/\/([,\%\w.\-_\/\?\=\+\&\~\#\$]+)/
    

    You will then need to add an if to your lamda function to see if there is anything in $matches[1] (Don't forget to increment your captures as well)

    You cannot use a negative look behind assertion here as the capture is not a fixed length, but you could use a negative look ahead assertion for the closing tag so it drops the entire match:

    /(<a[^>]*>)?http:\/\/([,\%\w.\-_\/\?\=\+\&\~\#\$]+)(?!<\/a>)/
    
    0 讨论(0)
提交回复
热议问题