1

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>
1
  • Okay, what's your question? What have you tried? Commented May 20, 2015 at 3:18

4 Answers 4

2

You can use the following to match:

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

And replace with #

Code:

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

See DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

Karthik, inside <a> tag, there can be classes defined. I tried putting a * betweeen <a and \s to extract those characters. It didn't work. My usecase is <a class="log" href="log.html#s1-s2-s3">. I will change the question accordingly but still mark this answer as accepted.
Welcome.. and what is the problem if you have classes defined? still you get the same answer? if no? can you give sample output and input?
the first comment was applicable to the first regex you gave, when you were extracting the groups... with the present answer, it works...
0

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

Comments

0

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,'#')

Comments

0

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

/href=".+"/g

DEMO

1 Comment

Could you provide a more complete example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.