How to replace existing image source with new one?

How to replace existing image source with new one?

I have a case where i need to replace the existing css with new css.

Please help me in using jQuery for the following

need to write jQuery for the following scenario:

**************
if 'src' of the 'Class XYZ' contains "word1" then replace it(src) with "\image1.gif"
if 'src' of the 'Class XYZ' contains "word2" then replace it(src) with "\image2.gif"

**************