regex:
/@([\S]*?(?=\s)(?!\. ))/g
given string:
'this string has @var.thing.me two strings to be @var. replaced'.replace(/@([\S]*?(?=\s)(?!\. ))/g,function(){return '7';})
expected result:
'this string has 7 two strings to be 7. replaced'
In case you want to make it “better” I’m trying to match Razor Html Encoded Expressions but mind the case about not matching an ending period followed by a space. The test case above shows that with the second (shorter) @var, whereas the first captures as @var.thing.me
Your pattern is not restrictive enough i.e., it captures too much. The last
@var.(including the dot) in your example string is captured because it is followed by a space (as required by the positive lookahead) which, in addition, is not followed by a dot and a space (as required by the negative lookahead). You can try this pattern:It will match the
@somethingsubstring (which can contain dot characters) both when it is followed by a space (as it happens in the first match of your string) and when it is followed by a dot and a space (as it happens in the second match of your string). Testing it in the chromium browser console it seems to work fine: