To answer your original question:
You can explecitely encode "have undergone nothing (beginning of line) or non-y character"
/(?:^|[^y])apple/
or depending on what you want to do, you can use word-boundary matching
/\bapple/
\b matches the boundary between the alphanumeric character (\ w) and the non-alphanumeric character (\ W)
In other environments (for example, Perl), sometimes there are convenient functions for searching:
/(?=)/ positive lookahead /(?!)/ negative lookahead /(?<=)/ positive lookbehind /(?<!)/ negative lookbehind
Regarding the issue of not replacing things inside pre
It might have been easier if you had done the two-step process: first remove all the text inside the <pre> tags from your line, then do the replacements and then add the hidden text after you are done.
source share