Skip to content Skip to sidebar Skip to footer

Url Replace With Anchor, Not Replacing Existing Anchors

I'm building code matching and replacing several types of patterns (bbCode). One of the matches I'm trying to make, is [url=http:example.com] replacing all with anchor links. I'm a

Solution 1:

Maybe something like this?

/(?:(?:ht|f)tps?:\/\/|www)[^<>\]]+?(?![^<>\]]*([>]|<\/))(?=[\s!,?\]]|$)/gm

And then trim the dots at the end if any.

regex101 demo

Though if the link contains more punctuations, it might cause some issues... I would then suggest capturing the link first, then remove the trailing punctuations with a second replace.

[^<>\]]+ will match every character except <, > and ]

(?![^<>\]]*([>]|<\/)) prevents the matching of a link between html tags.

(?=[\s!,?\]]|$) is for the punctuations and whitespace.

Solution 2:

Following regex should work. It's giving desired result on your sample inputs.

/((?:(?:ht|f)tps?:\/\/|www)[^\s,?!]+(?!.*<\/a>))/gm

See it in action here

(?!.*<\/a>) - Negative lookahead for anchor

Matching content will be stored in $1 and can be used in replace string.

EDIT

To not match content with <img src .. following can be used

(^(?!.*<img\s+src)(?:(?:ht|f)tps?:\/\/|www)[^\s,?!]+(?!.*<\/a>))

Solution 3:

can p.replace be a function? if so:

var text = 'http://www.example.com \n' +
           'http://www.example.com/test \n' +
           'http://example.com/test \n' +
           'www.example.com/test \n' +
           '<a href="http://www.example.com">http://www.example.com </a>\n' +
           '<a href="http://www.example.com/test">http://www.example.com/test </a>\n' +
           '<a href="http://example.com/test">http://example.com/test </a>\n' +
           '<a href="www.example.com/test">www.example.com/test </a>';
var p = {
    flag: true,
    pattern: /(<a[^<]*<\/a>)|((ht|f)tps?:\/\/|www\.).*?(?=\s|$)/ig,
    replace: function ($0, $1) {
                 if ($1) {
                     return$0;
                 } else {
                     p.flag =true;
                     return"construct replacement string here";
                 }
    }
};
while(p.flag){
    p.flag =false;
    text = text.replace(p.pattern, p.replace);
}

The part of the regex I added is (<a[^<]*<\/a>)| to check if the url is anywhere inside an anchor, if so then the replacement function ignores it.

If you want to avoid the url inside <a href="..."> but other urls inside the anchor are to be replaced, then change (<a[^<]*<\/a>)| to (<a[^>]*>)|

Post a Comment for "Url Replace With Anchor, Not Replacing Existing Anchors"