Saravana Kumar
Saravana Kumar

Reputation: 394

Regex pattern to replace and disable href links

I need to disable a link tags (whose href attribute value starts with log.html) in my html table. I am trying to use string replace to do.

The code line looks approximately like this, str.replace(/log.html...../g,'') where there must be a regex pattern in the place of dots.

All patterns like this,

<a class="log" href="log.html#s1-s1-s1"></a>
<a class="log" href="log.html#s1-s2-s100"></a>
<a class="log" href="log.html#s10-s5-s1"></a>

must be made as,

<a class="log" href="#"></a>

Upvotes: 1

Views: 753

Answers (4)

ChrisJL
ChrisJL

Reputation: 39

Since href and ".." are always available in a link, i would use a simple

/href=".+"/g

DEMO

Upvotes: 0

karthik manchala
karthik manchala

Reputation: 13640

You can use the following to match:

/log.html#[^"]*/g

And replace with #

Code:

str.replace(/log.html#[^"]*/g,'#')

See DEMO

Upvotes: 2

Jake Robers
Jake Robers

Reputation: 59

This regex pattern seems to work given that the url is accessable as a string. This can easily be accomplished with jQuery.

 str.replace(/log\.html.*/g,'#')

Upvotes: 0

Will Reese
Will Reese

Reputation: 2841

What you are looking for is string.match(). This function returns an array of the match and any captured groups. You could test all your links with something like this:

$('a').each(function() {
    href = $(this).attr("href");
    if(href.match(/^log\.html/)) {
        $(this).attr("href", "#");
    }
});

Fiddle

Upvotes: 0

Related Questions