EDIT
Simple case:
"fooBar".replace( /([a-z])([A-Z])/g, '$1-$2' ).toLowerCase();
"FooBar".replace( /([a-z])([A-Z])/g, '$1-$2' ).toLowerCase();
Edge case: this can get an extreme case where you have a single char.
"FooBarAFooBar".replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`)