1

Am newbie to regex am trying to do some regex replace function in java script here is my content and code

jQuery("td[headers='name_head']").each(function (index, value) { 
var text = jQuery(this).html();
if( text.indexOf('<a href=') >= 0){
jQuery(this).text(text.replace(/<a href=.*\"$/, "")); 

}
});

Html content will be look like this

 <a href="/sf/tracker/do/editField/projects.srk_test_pro_ject/tracker.Activity?fieldid=fild20226" class="DisableItemLink" onclick="javascript:return ((adminTrackerForm.hasChanges.value == 'true' &amp;&amp; confirm('You have unsaved changes to your field ordering. If you proceed, your ordering changes will be lost. Continue?')) || adminTrackerForm.hasChanges.value == 'false');">Calculate Points</a>

i just want to remove only the value inside href ""

Please throw some light on this

Regards Sathish

sathishkumar
  • 337
  • 1
  • 3
  • 10

3 Answers3

3

The text() method just retrieves the text contents which doesn't include any HTML tags. You can use html() method with a callback function where you can get the old HTML content as the second argument to the callback and based on the old value generate updated HTML.


The better way is to update the href attribute value of a tag to empty by directly selecting them, there is no need to loop over them since all values need to be empty.
jQuery("td[headers='name_head'] a").attr('href', '');

UPDATE 1 : In case you want to iterate and do some operation based on condition then do something like this.

jQuery("td[headers='name_head'] a").each(function(){
  if(//your ondition){
    $(this).attr('href', '');
  }
});

or

jQuery("td[headers='name_head']").each(function(){
  if(//your ondition){
    $('a', this).attr('href', '');
  }
});

UPDATE 2 : If you want to remove the entire attribute then use removeAttr('href') method which removes the entire attribute itself.

jQuery("td[headers='name_head'] a").removeAttr('href');
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
2

Why would you reinvent the wheel?

You don't need regex to achieve this, you can simply do it this way:

jQuery("td[headers='name_head'] a").attr('href', '');

It will set href to "" for all <a> elements inside td[headers='name_head'] so it will always respect your condition.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

I haven't tested this code; but something like this should help, don't think you need to use regex for this;

$('a.DisableItemLink[href!=''][href]').each(function(){
    var href = $(this).attr('href');
    // do something with href
})

This piece of code selects all elements which have the class DisableItemLink with a location set and sets it to blank.

I am curious as to what you are trying to do in the larger scheme of things though, sounds like there might be better ways to go about it.

Reference: some good selector combinations for links

Community
  • 1
  • 1
Nishan
  • 157
  • 1
  • 12
  • Thanks for your response Nishan actually am trying to remove the whole attribute href for some text inside td just the href contents – sathishkumar May 09 '17 at 15:15