shift 问号(?) 把点号(.) 转换成(?)

shift Question mark (?) a place left after converting dot (.) into (?)

我有将 (.) 转换为 (?) 的代码,但我想在将点 (.) 转换为 (?) 后将其移动一个位置,如果我们写 [abcdef .] 它应该变成 [abcdef? ] 在按下删除左侧 space 的点键后,如果我们写 [abcdef.] 它应该变成 [abcde?] 在按下删除左侧最近字符的点键后

<!doctype html>
<html dir='ltr' lang='en-GB'>

<head>
  <meta charset="UTF-8">
  <title>test page</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
 
<script language="javascript" type="text/javascript">

  
  $(function() {
  $('textarea').on("keyup", function(e) {
    if (e.key === '.') {
      const index = this.selectionStart;
      const text = $(this).val();
      if (index > 0 && text.charAt(index - 1) === '.') {
        $(this).val(text.substr(0, index - 1) + '?' + text.substr(index));
        this.selectionStart = index;
        this.selectionEnd = index;
      }
    }
  });
});
</script>
</head>

<body>
  <textarea></textarea>
</body>

</html>

只需更改:

$(this).val(text.substr(0, index - 1) + '?' + text.substr(index));

收件人:

$(this).val(text.substr(0, index - 2) + '?' + text.substr(index));

那一行是设置textarea的值,就是把原文的子串从0到'.'的位置。 (指数)。如果您更改为负 2,它将在“.”之前占用一个额外的字符。正如你所问。