首页 > 技术文章 > 用字母表中的位置替换每个字母。

mxyr 2018-06-28 13:44 原文

你需要给定一个字符串,用字母表中的位置替换每个字母。如果文本中的任何内容不是字母,请忽略它并不返回。

 1 function alphabetPosition(text) {
 2  let list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
 3  text = text.toLowerCase().replace(/[\W_]/g,'');
 4 console.log(text, 'b')
 5  let data = text.split('');
 6 console.log(data)
 7  let arr = [];
 8  data.forEach(function(ele) {
 9   for (let i = 0; i < list.length; i++) {
10    if (ele == list[i]) {
11     arr.push(i+1);
12    }
13   }
14  })
15   
16  console.log(arr, 'a')
17   
18  return arr.join(' ');
19 }
20 alphabetPosition("The narwhal bacons at midnight")

 

推荐阅读