I’m actually trying to delete every space in a small function statement excepted if the word “function” or “return” comes before it. This is what I tried but to no avail:
function hi() {
return "Hello";
}
hi.toString().replace(/\b\s+?=(return|function)/g, '');
There is no lookbehind in javascript regex, but this should do the trick:
This matches single spaces, eventually preceeded by function or return.
If the match is a single character, we replace it by an empty string. Else, we do not replace it.
Try it here: http://jsfiddle.net/ebY5w/2/