0

I have a problem with replace like this:

I have an array test :

test: Object
    boolean: "true"
    integer: "0"
    values|+|0: "option_1"
    values|+|1: "option_2"

and then i do parse like this:

for(var data in test){
   for(var input in test[data]){
   var input_aux = input.split('|+|');
   if(input != ''){
      $('table#'+table_id+' tbody td.'+input_aux[0]+' small').each(function(){ 
       var text_highlighted = $(this).text().replace(new RegExp('(' + test[table][input] + ')', 'gi'), '<b>$1<\/b>');
      $(this).html(text_highlighted);
    }}}

what i'm trying to accomplish is to match the data from the array like option_1 that is in that table exactly option_1 and change the html of it to <b>option_1</b>.

And it's working fine my problem is like when i have the same key but different value like in the example above, it will highlight only option_2 and can't understand why, any idea?

5
  • you may have to replace test[table][data] by test[data][input] ? Commented Apr 7, 2015 at 14:07
  • @stribizhev, please go in details, just to mention it's working fine for integer and any other single keys, thanks! Commented Apr 7, 2015 at 14:07
  • Do you mean my hint was enough to solve the issue? Changing replacement string from <b>$1<\/b> to <b>$1</b>? Commented Apr 7, 2015 at 14:13
  • Are the values in the same element? Commented Apr 7, 2015 at 14:14
  • @epascarello no , they are in different td > small but the td's are with the same id like td[class="values"] Commented Apr 7, 2015 at 14:15

1 Answer 1

1

The issue is the fact you are doing replacements even if there is no match. So you are reading the text() of all the elements and replacing it with that text. So you wipe out all of the existing html.

So check to see if there is a match before you do the replacement.

var re = new RegExp('(' + test[table][input] + ')', 'gi');
var txt = $(this).text();
if (txt.match(re)) {
    var text_highlighted = txt.replace(re, '<b>$1<\/b>');
    $(this).html(text_highlighted);
}

other option would be to use contains selector.

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

8 Comments

i'm reading only the $('table#'+table_id+' tbody td.'+input_aux[0]+' small'), so just the td's that have class included in the key of the array, is that an issue? i'm not reading all the table
This is your problem.
can you please give me more details, i really don't get my problem in the approach, sorry
You replace every element you loop through with the text(). The text() removes all the html in that element so you blow away the previous match!
@ epascarello --- Amazing answer, i wasn't really sure that somebody will understand what i wrote in the question, but you got the issue and save the situation, Thanks man! worked like a charm.
|

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.