1

Is there an easy way to fix this code:

title_1 = $(this).closest('tr').find('td').html();
title_2 = $(this).closest('tr').find('td').next().html();
title_3 = $(this).closest('tr').find('td').next().next().html();

question = question.replace(/{title_1}/g, title_1);
question = question.replace(/{title_2}/g, title_2);
question = question.replace(/{title_3}/g, title_3);

So it isn't so dully (repeated) and can cover n occurences of title_ pattern?

I'm a beginner Javascript developer and a complete regular expressions newbie (actually, they scare me! :|), so I'm unable to do this by myself. I've tried to look for an inspiration in different languages, but failed.

4 Answers 4

2

You can use a function in the replace, to get the value depending on what you find:

question = question.replace(/{title_(\d+)}/g, $.proxy(function(x, m){
  return $(this).closest('tr').find('td:eq('+(m-1)+')').html();
}, this));

Demo: http://jsfiddle.net/n3qrL/

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

2 Comments

m should be the second parameter.
@xdazz: Yes, you are correct. Thanks for spotting that.
2

String.prototype.replace() could take a function as second parameter.

var $this = $(this);
question = question.replace(/\{title_(\d+)\}/g, function(match, n) {
  return $this.closest('tr').find('td').eq(n - 1).html();
});

Comments

1

Demo Here

Try this , Generalized for getting all td tag's text value :

$("table").find("tr").each(function(){

 $(this).find("td").each(function(){
     alert($(this).html());
     var txt=$(this).html();
     //var pattern="/{"+txt+"}/g";
     //question = question.replace(pattern, txt);
 });
});

NB. In your question you have not mentioned the value for 'question' . please define value for 'question'

1 Comment

Yeap, you're right! I was always told to paste only imporant parts of code, when asking questions in SE.
1

It seems to me that you want to get the text content of the first three cells of a table row and use it to replace the content of a string, and that this is an element somewhere in the row. So you can do:

var n = 3;  // number of cells to get the text of
var textArray = [];
var tr = $(this).closest('tr')[0];
var reString;

for (var i=0; i<n; i++) {
  reString = '{title_' + (i+1) + '}';
  question = question.replace(reString, tr.cells[i].textContent);
}

If you wish to avoid jQuery's closest, you can use a simple function like:

function upTo(el, tagName) {
  tagName = tagName.toLowerCase();
  do {
    el = el.parentNode;
    if (el.tagName && el.tagName.toLowerCase() == tagName) {
      return el;
    }
  } while (el.parentNode)
}

then:

var tr = upTo(this, 'tr');

1 Comment

Yeap, you're right! The only difference (as exposed in last question's paragraph) is, that I want this for n cells of a table row, no for just the three first.

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.