Javascript base64编码

Javascript base64 encode

我有这个凭证:

username: pippo
password: 1234\zxcv

我需要生成 base 64 编码。正确的 base 64 字符串是

我使用 btoa() 但没有正确转换字符串。 问题是 \ 字符。我能逃脱吗?

谢谢

当用户输入 \ 个字符时,用 \ 转义 \

<form>
username: <input type="text" /><br />
password: <input type="password" /><br />
<input type="submit" />
</form>
<script>
  var input = document.querySelector("input[type=password]");
  input.oninput = function(e) {
    if (e.target.value.slice(-1) === "\") {
      e.target.value = e.target.value.replace(/\/g, "\");
    }
  }
  
  document.querySelector("input[type=submit]")
  .onclick = function(e) {
    e.preventDefault();
    console.log(btoa(input.value), atob(btoa(input.value)));
  }
</script>