Basically you would want to isolate the search terms into an array.
Then write a function that probably takes the search term array and the starting text as an argument and returns the text with the added spans.
Then you would use a regular expression match while looping through the array to do the replacement. Since you only pass in the content, there are no html tags to ignore, but you could use a regular expression to remove tags first before doing replacements if necessary as well.
Please add this to feature request list
2 options, 1 feature
Highlight part words
Highlight only complete words
Use different colours to highlight different words in search list