Why is regex working only if function scoped variable is declared after the local doCount function

I have some sample code at the following link: working sample.

When I click on the the button that says Remove new line character(s) 1 the new line character is not trimmed from the string a. However, when I click on that says Remove new line character(s) 2 then the new line character in string a gets removed. Both these buttons are calling different functions when they are clicked, but the difference between these methods is simply that the regex expression variable _trimNewLineCharsRegExp is declared before doCount local scoped function.

Question
Why is the new line character not being removed by the first button onclick function?