Are you sure you copied the expression correctly? You get everything between parentheses subscripted because both entire left ("lookbehind") and right ("lookahead") expression contain lots of unique chemical elements, separated by | ('OR') but also within square brackets ([..]). This notation is only and exclusively used for a Single Character Set. So the entire expression
actually checks for one single occurrence of one the characters '(' 'N' 'a' ')' '|' 'C' 'l' 'H' .. and so on. Note this includes the single parentheses, before and after. You can also see this when you type "a2a" – the '2' will get subscripted.
You should remove the square brackets, but then you end up with a lookbehind with elements of variable length (one part is "Na", another is "H") which is not supported by InDesign. To fix that, you need to split up the lookbehind into two parts: one that looks for 2 characters OR one that looks for 1 character.
Due to the nature of chemical notation, I don't think you need the lookahead at all! The following regex will match the test compounds you mention:
and it will refuse to fire when one of these letters are followed by more than 3 digits – that's what the negative lookahead is for.
It's tempting to devise a regex "(?<=\d [cmk]?m)\b" (which checks for 'm' with an optionally 'c', 'm', or 'k' prefix) but, again, it will not work because of the variable length. In this case it's the question mark ('?') that causes the length to be variable: after all, it means "either zero or once". But you can duplicate the lookbehinds again, and end up with a regex which is longer and more cumbersome, but at least works:
((?<=\d [cmk]m)|(?<=\d m))\b
Don't forget the Ununtrium! …
Sorry for the late reply, but that is exactly right!
Many thanks for the explanations as well, since it clears up why the code I was using didn't work. Especially about the variable lengths, I did not know that... And the first code you wrote with the bonus was the one I tried too . Because it seemed so logical.
With this newfound knowledge I'll be able to tackle other problems that might come up.
Thanks again Jongware!