简体   繁体   中英

Regex for finding all text urls but not in images src

I'm replacing all text urls on page into clickable.
When i do this it also replaces links in 'src' attribute of images (which is not desired)

My regex is this

[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?

I tried negative search with

(?!src\=\")[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?

but it dosn't match a thing.

How should i precede my regex?

How about something like this...

(?:(href="))([-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?)(?:([^>]*>[^<]*</a>))

I added non-capturing groups to the beginning and end to verify that it's an anchor href. I tested it with this page's source at http://regexpal.com/

I'm not sure about the question, but my interpretation is that you want to replace the value of href attribute in A elements.

This regex will find A elements that have an href attribute. The value will be in the first capture group.

(?:<a[^>]*?href\s*=\s*")([^"]*)(?:")

Here is a version capturing all three parts of the match and using a replace function

myHtml.replace(/(<a[^>]*?href\s*=\s*")([^"]*)(")/gi, function(match, $1, $2, $3) {
    return $1 + "javascript.void(functionName('" + $2 + "'))" + $3;
});

if myHtml contained something like the following:

<a target="new" title="test" href="http://url.com">

it would get converted into this:

<a target="new" title="test" href="javascript.void(functionName('http://url.com'))">

You also mentioned clickable.

myHtml.replace(/(<a[^>]*?)(href\s*=\s*"([^"]*)")/gi, function(match, $1, $2, $3) {
    return $1 + "onclick=\"functionName('" + $3 + "')\"";
});

results in:

<a target="new" onclick="functionName('http://url.com')">

Btw, I tested this with my own regex tool: http://rey.gimenez.biz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM