Skip to content Skip to sidebar Skip to footer

Javascript Regexp That Matches '.' Not Preceded By '\' (lookbehind Alternative)

I am making a (Excel like) number formater function in javascript. I want to use templates like '0 000.00' and '000.000.000' which would produce: format(123456789,'0 000.00') >&

Solution 1:

Reverse the string and use a negative lookahead instead.

template.split("").reverse().join("")
        .split(/\.(?!\\)/)
        .split("").reverse().join("");

That's a "fun" way of doing it but for your case there are other ways that may be better. Like replacing all \. with a magic string like __MAGIC__, splitting by ., then undoing the magic strings.

Post a Comment for "Javascript Regexp That Matches '.' Not Preceded By '\' (lookbehind Alternative)"