Searching the clipboard & changing a portion of it

Is there a way to change the contents of a portion of the clipboard based on the following criteria:

First off, before my request I am copying a bunch of html code to the clipboard.

Wish list:
The script will then search the clipboard contents for a start and end string:

startString = “<!-- Address 1 Start”
endString = “<!-- Address 9 End”

Then replace the clipboard contents with a stored string of code, replacing just the lines of code between the stratString and endString with my storedString? Better yet would be to replace the start and end strings as well. But it has to know where to begin and end.

Or maybe, there’s a post that somebody knows of that explains how this could be achieved?

Thanks,
-Jeff

Something like this:

set theClip to the clipboard
set endString to "<!-- Address 9 End"
set theStart to offset of "<!-- Address 1 Start" in theClip
set theEnd to offset of endString in theClip
set theClip to text 1 thru (theStart - 1) of theClip & "<!-- replacement text here..." & text (theEnd + (length of endString)) thru -1 of theClip
set the clipboard to theClip

Awesome! That should work. “the offset”. that was key! Thank you very much Shane! Even if it does not work, that is great to know.

Thanks again,
-Jeff

Once again, this is superb. I have an additional and probably easy question.
What is the fastest way to verify if a string exists within the clipboard set to text?

For example, I would like to throw and error if the clipboard does not contain:

“<!-- Address 1 Start”

or

“<!-- Address 9 End”

Do I have to use text item delimiters? - Which is fine, if I have to. I just am not sure if there is a more optimal way.

Just after the instruction extracting its content from the clipboard, insert this new one :

if (theClip does not contain "<!-- Address 1 Start") or (theClip does not contain "<!-- Address 9 End") then error "The required strings are missing"

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mercredi 30 septembre 2015 16:57:14

Good grief. I tried that in the past with no success. Needless to say, the document I was copying to the clipboard was flawed, skewing my find results. Thank you so much Yvan for making me look deeper and detecting the issue and allowing me to use this simple clean code. I can’t thank you both enough for the help you provide me.

-Jeff