我想使用现代JavaScript语法检索一个人的名字和姓氏的首字母.目前,我能够正确检索名字的第一个字母和姓氏的第一个字母.

我的问题是,如果有多个名字,我就无法正确检索它.

Code

const names = ["Joshua Jones of USA", "William Sanders", "Grey Charles", "Carlos James Thomas of Russia", "Peter John Parker", "Juan Carlos Tenaz Lopez III of Spain"];

function getUserInitials(name) {
  return name.split(' ').map(w => w.charAt(0).toUpperCase()).splice(0, 2);
}

for (const name of names)
  console.log(getUserInitials(name));

Expected Output

JJ、WS、GC、CT、PP、JL

推荐答案

在我的解决方案中,我将名称完全按照连词分开,并只保留开头.随后,对于每个由多个大写字母组成的单词,我也将它们分开.根据这些名称,我连接第一个和最后一个大写字母并返回结果.

解决方案#1

function getUserInitials(name) {
  // Clean up the name: remove titles, conjunctions, and punctuation
  // Carlos James Thomas of Russia ---> Carlos James Thomas
  name = name.replace(/(?:\b(?:of|the|and|in|on|at|for|with)\b|[,\.]).*$/gi, '');
  
  // Remove all-uppercase words (e.g., III)
  // Juan Carlos Tenaz Lopez III, Mexico ---> Juan Carlos Tenaz Lopez
  name = name.replace(/\b[A-Z]+\b.*$/g, '');
  
  // Remove whitespaces
  name = name.trim();
  
  // Get names
  const words = name.split(' ');

  // Get first char from First and Last name
  let result = '';
  result += words[0].charAt(0).toUpperCase();
  if (words.length > 1) result += words[words.length - 1].charAt(0).toUpperCase();

  return result;
}

// Test cases
const names = [
  "Joshua Jones of USA",
  "William Sanders",
  "Grey Charles",
  "Carlos James Thomas of Russia",
  "Peter John Parker",
  "Juan Carlos Tenaz Lopez III of Spain",
];

for (const name of names) {
  console.log(getUserInitials(name));
}

解决方案#2

我们不要过滤掉完全大写的单词,而只过滤掉罗马数字.

function getUserInitials(name) {
  // Clean up the name: remove titles, conjunctions, and punctuation
  // Juan Carlos Tenaz Lopez III of Spain ---> Juan Carlos Tenaz Lopez III
  name = name.replace(/(?:\b(?:of|the|and|in|on|at|for|with)\b).*$/gi, '');

  // Remove Jr. / Sr.
  name = name.replace(/\b(?:Jr|Sr)\.?\b/g, '').replace(/\s*\./g, '');
  
  // Remove Roman numerals
  // Juan Carlos Tenaz Lopez III ---> Juan Carlos Tenaz Lopez
  // Juan Carlos Tenaz Lopez III Another Name ---> Juan Carlos Tenaz Lopez  Another Name
  name = name.replace(/\b(?:M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}))\b/g, '');
  
  // Just tip: After removing the Roman numerals, double spaces were left in place, so from there, we can trim the string if desired
  // Juan Carlos Tenaz Lopez  Another Name ---> Juan Carlos Tenaz Lopez
  name = name.replace(/\s{2}.*$/g, '');
  
  // Remove whitespaces
  name = name.trim();
  
  // Get names
  const words = name.split(' ');

  // Get first char from First and Last name
  let result = '';
  result += words[0].charAt(0).toUpperCase();
  if (words.length > 1) result += words[words.length - 1].charAt(0).toUpperCase();

  return result;
}

// Test cases
const names = [
  "Joshua Jones of USA",
  "William Sanders",
  "Grey Charles",
  "Carlos James Thomas of Russia",
  "Peter John Parker",
  "PETER JOHN PARKER",
  "Jr. Peter Parker",
  "Jr Peter Parker",
  "Sr. Peter Parer",
  "Sr Peter Parker",
  "Juan Carlos Tenaz LOPEZ I",
  "Juan Carlos Tenaz LOPEZ MMM EXAMPLE",
  "Juan Carlos Tenaz LOPEZ I of Spain",
  "Juan Carlos Tenaz LOPEZ II of Spain",
  "Juan Carlos Tenaz LOPEZ III of Spain",
  "Juan Carlos Tenaz LOPEZ IV of Spain",
  "Juan Carlos Tenaz LOPEZ V of Spain",
  "Juan Carlos Tenaz LOPEZ VI of Spain",
  "Juan Carlos Tenaz LOPEZ IX of Spain",
  "Juan Carlos Tenaz LOPEZ X of Spain",
  "Juan Carlos Tenaz LOPEZ XVI of Spain",
  "Juan Carlos Tenaz LOPEZ XXX of Spain",
  "Juan Carlos Tenaz LOPEZ XL of Spain",
  "Juan Carlos Tenaz LOPEZ CLX of Spain",
  "Juan Carlos Tenaz LOPEZ CD of Spain",
  "Juan Carlos Tenaz LOPEZ CM of Spain",
  "Juan Carlos Tenaz LOPEZ MMMCDXVIII of Spain",
];

for (const name of names) {
  console.log(getUserInitials(name));
}

Javascript相关问答推荐

IOS(React Native)中未找到模块SquareReaderSDK

React:如何将表格收件箱正确渲染为表格主体内的组件?

我可以后增量超过1(最好是内联)吗?

Angular material 表多个标题行映射

. NET中Unix时间转换为日期时间的奇怪行为

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

Promise Chain中的第二个useState不更新

保持物品顺序的可变大小物品分配到平衡组的算法

如何在Angular17 APP中全局设置AXIOS

从页面到应用程序(NextJS):REST.STATUS不是一个函数

为什么云存储中的文件不能公开使用?

Phaserjs-创建带有层纹理的精灵层以自定义外观

OnClick更改Json数组JSX中的图像源

Cherrio JS返回父div的所有图像SRC

Pevent触发material 用户界面数据网格中的自动保存

每隔3个项目交替显示,然后每1个项目交替显示

不协调嵌入图片

是否设置以JavaScript为背景的画布元素?

连续添加promise 时,如何在所有promise 都已结算时解除加载覆盖

JQuery使用选项填充HTMLSELECT并设置默认结果,默认结果显示为空