What is a method that can be used to increment letters?

前端 未结 14 2825
我在风中等你
我在风中等你 2020-11-27 04:19

Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers a method of incrementing a letter?

I would like to be able to do somet

14条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 04:30

    Here is a variation of the rot13 algorithm I submitted on https://stackoverflow.com/a/28490254/881441:

    function rot1(s) {
      return s.replace(/[A-Z]/gi, c =>
        "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza"[
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".indexOf(c) ] )
    }
    

    The input code in the bottom and the looked up codec is on the top (i.e. the output code is the same as the input code but shifted by 1). The function only changes letters, i.e. if any other character is passed in, it will be unchanged by this codec.

提交回复
热议问题