将所有拉丁字母替换为google-app-script中的数字?

时间:2019-06-04 13:45:14

标签: javascript

当我尝试使用该功能时,我会尝试显示aplhabet中的每个字母

it outputs ("01bcdefghijklmnoprstuvwqxyz").

所以它只能变成字母 “ a”代表数字“ 01”。其余字母保持不变。我正在尝试专门用于对字母进行后期处理,因此在执行代码时仅显示数字。

这是我尝试过的代码:

function letters_to_numbers(st)
{
a = st.replace("a", "01");
  return a;
b = st.replace("b", "02");
return b;
c = st.replace("c", "03");
  return c;
d = st.replace("d", "04");
  return d;
e = st.replace("e", "05"); 
  return e;
f = st.replace("f", "06");  
  return f;
g = st.replace("g", "07"); 
  return g;
h = st.replace("h", "08"); 
  return h;
i = st.replace("i", "09"); 
  return i;
j = st.replace("j", "10"); 
  return j; 
k = st.replace("k", "11");  
  return k;
l = st.replace("l", "12"); 
  return l;
m = st.replace("m", "13"); 
  return m;
n = st.replace("n", "14");   
  return n;
o = st.replace("o", "15"); 
  return o;
p = st.replace("p", "16");
  return p;
r = st.replace("r", "17");
  return r;  
s = st.replace("s", "18");
  return s; 
t = st.replace("t", "19");
  return r;
u = st.replace("u", "20");
  return u; 
v = st.replace("v", "21");
  return v; 
w = st.replace("w", "22");
  return w;   
q = st.replace("q", "23");
  return q; 
x = st.replace("x", "24");
  return x;  
y = st.replace("y", "25");
  return y; 
z = st.replace("z", "26");
  return z;  
}

7 个答案:

答案 0 :(得分:2)

return语句停止执行功能。

相反,请尝试将每次执行代码的结果添加到一个空数组中,我将为此目的遍历数组而不是对其进行26次以上的硬编码。

如果您想了解有关return语句的更多信息: https://www.w3schools.com/jsref/jsref_return.asp

如果您想了解更多有关for循环的信息: https://www.w3schools.com/js/js_loop_for.asp

答案 1 :(得分:1)

您的函数在找到任何字母的匹配项后立即“返回”。由于“ a”在您的测试阶梯中排在首位,因此任何包含“ a”的字符串都只会被替换。

函数的每个部分都对原始字符串进行操作,您需要对其进行更改,以使每个字符串都知道上一个测试所做的更改。

您还需要使用g标志将RegExp设置为“全局”,否则它将只替换任何字母的第一个出现的位置。

答案 2 :(得分:1)

一旦a被替换,您将立即从函数返回。相反,您需要每个字符replace,并且仅在替换z后返回:

function letters_to_numbers(st) {
  st = st.replace("a", "01");
  st = st.replace("b", "02");
  st = st.replace("c", "03");
  ...............
  ...............
  st = st.replace("z", "26");

  return st;
}

另一种替代方法是根据所有a-z个字符的charCode来replace个字符。

a-> 97和b-> 98的字符,依此类推。因此,m.charCodeAt(0) - 96返回一个介于1-26之间的数字。然后,您可以使用padStart来获取一位数字的前缀0

function letters_to_numbers(st) {
  return st.replace(/[a-z]/g, m => (m.charCodeAt(0) - 96).toString().padStart(2, 0))
}
console.log(letters_to_numbers("abcdefghijklmnoprstuvwqxyz"))
console.log(letters_to_numbers("lorem ipsum"))

答案 3 :(得分:0)

返回结束函数。

直到最后都不会返回任何东西。

更新:射击版本

如果您想对字母进行加密或其他加密处理,只需更改alphabet中字母的顺序即可。

function letters_to_numbers(st)
{
    var updatedString = st + '';
    var alphabet = 'abcdefghijklmnopqrstuvwxyz';

    for (var i = 0; i < alphabet.length; i++) {
        var letter = alphabet.charAt(i);
        var number = i.toString().padStart(2, '0');
        updatedString = updatedString.replace(letter, number);
    };

    return updatedString;
}

旧方法,这可以帮助您入门:

function letters_to_numbers(st)
{
    let updatedString;
    updatedString = st.replace("a", "01");
    updatedString = updatedString.replace("b", "02")
    updatedString = updatedString.replace("c", "03");
    updatedString = updatedString.replace("d", "04");
    updatedString = updatedString.replace("e", "05"); 
    updatedString = updatedString.replace("f", "06");  
    updatedString = updatedString.replace("g", "07"); 
    updatedString = updatedString.replace("h", "08"); 
    updatedString = updatedString.replace("i", "09"); 
    updatedString = updatedString.replace("j", "10");  
    updatedString = updatedString.replace("k", "11");  
    updatedString = updatedString.replace("l", "12"); 
    updatedString = updatedString.replace("m", "13"); 
    updatedString = updatedString.replace("n", "14");   
    updatedString = updatedString.replace("o", "15"); 
    updatedString = updatedString.replace("p", "16");
    updatedString = updatedString.replace("r", "17");  
    updatedString = updatedString.replace("s", "18"); 
    updatedString = updatedString.replace("t", "19");
    updatedString = updatedString.replace("u", "20"); 
    updatedString = updatedString.replace("v", "21"); 
    updatedString = updatedString.replace("w", "22");   
    updatedString = updatedString.replace("q", "23"); 
    updatedString = updatedString.replace("x", "24");  
    updatedString = updatedString.replace("y", "25"); 
    updatedString = updatedString.replace("z", "26"); 
    return updatedString; 
}

答案 4 :(得分:0)

string.replace()仅在您的模式是字符串时才替换第一个匹配项。使用带有全局标志的常规表达来匹配多次。另外,您可以使用函数作为替换值来动态确定替换项。

您可以使用查找对象将字母映射为数字,也可以仅使用ASCII字符代码。 97122均为小写。只需从字符的字符码中减去96,然后用零填充以获取您的数字。

// Adds two zeroes ahead of the number, then take the last two digits.
const pad = n => `00${n}`.slice(-2)

const lettersToNumbers = s => {
  // Match all lowercase letters.
  return s.replace(/[a-z]/g, m => {
    // For each match, get the character code.
    // Subtract by 96 so that "a" is 1.
    return pad(m.charCodeAt(0) - 96)
  })
}


console.log(lettersToNumbers('abcdefghijklmnopqrstuvwxyz'))

答案 5 :(得分:0)

这个问题似乎不值得花这么多代码:

const strToNum = str => [...str]
  .map(char => 
    ([...'abcdefghigklmnopqrstuvwxyz'].indexOf(char)+1)
    .toString()
    .padStart(2,0))
  .join('');

console.log(strToNum('test'));

答案 6 :(得分:0)

这是实现此目标的另一种方法

const input = "abcdefghijklmnoprstuvwqxyz";

const letterToNumber = (char) => {
  const initialValue = 'a'.charCodeAt(0);
  num = char.charCodeAt(0) - initialValue + 1;
  return num < 10 ? '0'+num : num;
}

let output = input.split('').reduce((convertedString, char) => {
  convertedString += letterToNumber(char);
  return convertedString;
},'')

console.log(output)