鍍金池/ 問答/GO  HTML/ 如何把名字中間的字變成星號(hào)?

如何把名字中間的字變成星號(hào)?

李*平
慕容*霜

回答
編輯回答
朕略傻

正則版的來了:

"李某平".replace(/^(.+).(.)$/, "$1*$2");
"慕容某霜".replace(/^(.+).(.)$/, "$1*$2");
2017年4月13日 20:45
編輯回答
脾氣硬

牛逼一點(diǎn)的方法是寫個(gè)正則替換一下,low逼一點(diǎn)的就是判斷一下長度,處理一下

var name = '李王平'
nameArr = name.split('')
if (nameArr.length  > 2)
{
 nameArr[nameArr.length - 2] = '*'
}
return nameArr.join('')
2017年8月8日 05:02
編輯回答
忘了我
function protectionName(name) {
  return [...name]
    .map((item, index, arr) => {
      return Math.floor(arr.length / 2) === index ? '*' : item;
    })
    .join('');
}

protectionName('李和平'); // 李*平
protectionName('慕容冰霜'); // 慕容*霜
2018年6月10日 01:10