【问题标题】:Letter replace in textbox文本框中的字母替换
【发布时间】:2017-07-10 15:50:17
【问题描述】:

我正在尝试重新创建一个最终类似于rot13.com 的文本转换器。但是我不知道如何使它将特定字符转换为另一个字符。如果我想输入;

例子

那么我希望输出是

rcszqar

还有一些;

abc > sv

foo > gpp

bar > nst

我正在使用return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + i) ? c : c - 26);

但是无论我在该行中更改什么数字,它们都没有被正确替换。

【问题讨论】:

    标签: html replace character inputbox


    【解决方案1】:

    这里记录了该函数: http://stackoverflow.com/a/617685/987044

            function rot(s, i) {            return s.replace(/[a-zA-Z]/g, function (c) {
                    return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + i) ? c : c - 26);            });         }       function update() {             document.getElementById('output').value = rot(document.getElementById('input').value,
    +document.getElementById('rot').value);         }
    

    调用函数时唯一需要更改的数字是变量i。例如

    rot ("example", 13);//->"rcszqar"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2015-11-03
      • 1970-01-01
      相关资源
      最近更新 更多