0

Possible Duplicate:
Find text string in jQuery and make it bold

I have some text on my html page (with accents on leters) :

<p>
some téxt, some text, some téxt, some text, some text, some téxt, 
</p>

My goal is to set bold style for all occurences of "text" and "téxt"

Is it possible ?

thank you

Valeriane

Community
  • 1
  • 1

2 Answers2

0

You can't do the following in your case,

str.replace(/(t.xt)/g, "<b>"+text+"</b>);

so we need to use a callback function :

//Assuming you can retrieve the text from the p tag or from the entire page
var str = "some téxt, some text, some téxt, some text, some text, some téxt, " 

str.replace(/(t.xt)/g, function(match){
  return "<b>"+match+"</b>";
})

You can add this snippet to document.onload or $().function(){...}

Mudassir Ali
  • 7,913
  • 4
  • 32
  • 60
  • text and téxt are used as an example, I need something functional in any case –  Nov 05 '12 at 14:48
  • This should help http://stackoverflow.com/questions/5700636/using-javascript-to-perform-text-matches-with-without-accented-characters – Mudassir Ali Nov 05 '12 at 14:56
0

Assuming your text is in:

<p id="txt">
some téxt, some text, some téxt, some Text, some text, some téxt, xyz
</p>



var bold = ['some','text', 'xyz']; // words to bold
var content = document.getElementById('txt');//content to find those words
var regExp = new RegExp('('+bold.join('|')+')', "gi"); //build regular expression (i means case insensitive)

content.innerHTML = content.innerHTML.replace(regExp, "<strong>$1</strong>"); //find and bold
Adam Bubela
  • 9,433
  • 4
  • 27
  • 31
  • 1
    text and téxt are used as an example, I need something functional in any case –  Nov 05 '12 at 15:42