Javascript拆分为$,但不包括$$

Eoi*_*ray 1 javascript regex

var s= "this is inline $\alpha$, $$not$$";
Run Code Online (Sandbox Code Playgroud)

我怎样才能更换'$'by '%%'而不是'$$'.

这样输出就是

var s= "this is inline %%\alpha%%, $$not$$";
Run Code Online (Sandbox Code Playgroud)

我刚在想

s.split('$').join('%%')
Run Code Online (Sandbox Code Playgroud)

但我需要分成一美元而不是两美元.

Fel*_*ing 9

你可以使用回调和贪心量词:

s.replace(/\$+/g, function(match) {
    return match.length === 1 ? '%' : match;
});
Run Code Online (Sandbox Code Playgroud)