How to change the words in the middle of a name into an asterisk?

Li * Ping
Murong * Cream

Mar.03,2021

the better way is to write a regular replacement, and what low forces is to judge the length and deal with it

.
var name = ''
nameArr = name.split('')
if (nameArr.length  > 2)
{
 nameArr[nameArr.length - 2] = '*'
}
return nameArr.join('')

"".replace(/^(.+).(.)$/, "$1*$2");
"".replace(/^(.+).(.)$/, "$1*$2");
Menu