Find 'n' Replace

I found this shell command (Code Exchange) to replace text very fast
But if your text contains a ’ (single quot mark) and the search text happens to contain a ’ too you get an error.
How can this be handled? I cannot believe that a simple singlequot can make “sed” fail!

set txt to quoted form of "this is just a test text"
set srch to quoted form of "test"
set repl to quoted form of "foo"
set txt to (do shell script "echo " & txt & "| sed -e 's|" & srch & "|" & repl & "|g' ")

Vince
(*nix noob)

Then I should go for old method like here.

on stringReplace(theText, oldString, newString)
	set {AppleScript's text item delimiters, oldDelimiter} to {oldString, AppleScript's text item delimiters}
	set theText to every text item of theText
	set AppleScript's text item delimiters to newString
	set theText to theText as string
	set AppleScript's text item delimiters to oldDelimiter
	return theText
end stringReplace