我希望它显示“博士。“房子”而不是医生。Greg House和我只允许修改“const re = /^(\w+.\s)(\w+\s\w+)$/;“谁能帮帮我?
这里是代码
const userName = "Dr. Greg House"; // Code will also be tested with "Mr. Howard Wolowitz"
/* Your solution goes here */
const re = /^(\w+\.\s)(\w+\s\w+)$/;
const result = re.exec(userName);
console.log(result[1] + " " + result[2]);
2条答案
按热度按时间cs7cruho1#
仅对需要保留的单词使用捕获组:
xfb7svmp2#
.replace()
与.trim()
结合使用也可以完成这项工作: