You can match two or more whitespaces and replace with the same amount of hyphens:
const s = 'I love to eat cake'
console.log(s.replace(/\s{2,}/g, (x) => '-'.repeat(x.length)) )
The same approach can be used in Python (since you asked), re.sub(r'\s{2,}', lambda x: '-' * len(x.group()), s), see the Python demo.
Also, you may replace any whitespace that is followed with a whitespace char or is preceded with whitespace using
const s = 'I love to eat cake'
console.log(s.replace(/\s(?=\s|(?<=\s.))/gs, '-') )
console.log(s.replace(/\s(?=\s|(?<=\s\s))/g, '-') )
See this regex demo. Here, s flag makes . match any char. g makes the regex replace all occurrences. Also,
\s - matches any whitespace
(?=\s|(?<=\s.)) - a positive lookahead that matches a location that is immediately followed with a whitespace char (\s), or (|) if it is immediately preceded with a whitespace and any one char (which is the matched whitespace). If you use (?<=\s\s) version, there is no need of s flag, \s\s just makes sure the whitespace before the matched whitespace is checked.