Use jQuery to find and replace multiple words on t

2019-09-21 10:11发布

问题:

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"

回答1:

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] ) );
    }
});