-2

Possible Duplicate:
Find and Replace more than 1 word?

I want to use jQuery to replace all instances of specific words. For example, I want to replace "My Classes" to "My Levels" and "My Class" to "My Level". I also want to change "college" to "university"

Community
  • 1
  • 1
Nate Allen
  • 238
  • 5
  • 16
  • Searching this forum gave me: http://stackoverflow.com/questions/2349446/jquery-find-and-replace-text-after-body-was-loaded and http://stackoverflow.com/questions/2349138/jquery-find-and-replace-text-without-element-id – papkass Mar 28 '12 at 13:39
  • I've tried other examples, but they all just replace one word (X with Y) and I have need to replace multiple sets of words (X with Y, A with B, 1 with 2, etc) – Nate Allen Mar 28 '12 at 13:46

1 Answers1

2

You may try something like this:

var dictionary= {
"My Classes":"My Levels",
"My Class":"My Level",
"college":"university"
};

$("body *").each( function(){
    for( var ptrn in dictionary){
        $(this).text( $(this).text().replace(new RegExp(ptrn ,"g"), dictionary[ptrn] ) );
    }
});
Engineer
  • 47,849
  • 12
  • 88
  • 91