【问题标题】:How to Remove Character ↵ in between two words of JavaScript String?如何在 JavaScript 字符串的两个单词之间删除字符 ↵?
【发布时间】:2019-10-08 13:00:01
【问题描述】:

如果我同时选择两行并使用window.getSelection().toString() 获取文本,那么我会在两个单词之间得到(上一行的最后一个单词和下一行的第一个单词)。以下是我的代码-

document.addEventListener('dblclick',function (event) {

var element = document.getElementsByClassName(event.target.parentElement.parentElement.parentElement.className);


var T= window.open("", "MsgWindow", "width=200,height=500");

T.document.write(element[0].innerText.replace(/\n/g, "<br/>"));


T.document.write(`<script>
window.addEventListener("keydown", function(e){
if(e.keyCode === 16) {
var text = "";
if (window.getSelection) {
text_1 = window.getSelection().toString();
text = text_1.replace(/\u21b5/g," "); // my attempt to corrrect
                        } 
words = text.split(" ");
console.log(words);
}
},false);
</script>`);
},false);

例如,如果我选择 -

侧面

部分

然后按shift键我得到-

Array(1)
0: "Sides↵Section"
length: 1
__proto__: Array(0)

但输出应该是-

Array(1)
0: "Sides Section"
length: 1
__proto__: Array(0)

如何将 替换为空格?

PS:我根据SE上给出的解决方案尝试了一些可能性,但没有奏效,请不要认为这是微不足道的或重复的帖子。

【问题讨论】:

  • 我试过了,但没有得到我想要的结果
  • 换行符就是这样显示的,有很多方法可以去掉。试试variable.replace("\n", " ")
  • @GammaGames 我试过了,你自己检查一下,还是不行,不知道是什么原因!
  • @Mike 人们试图提供帮助,而您的 cmets 可能会被视为粗鲁。

标签: javascript replace


【解决方案1】:

我想你会想要替换新行回车

所以是这样的:

text = text_1.trim().replace(/[\n\r]/g, ' ');

更新

// Solution from code sample 

  T.document.write(`<script>
    window.addEventListener("keydown", e => {
      if (e.keyCode === 16) {
        if (window.getSelection) {
          const text = window.getSelection().toString();
          const words = text.trim().replace(/\r?\n/g, " ");
          console.log(words);
        }
      }
  });
  </script>`);

【讨论】:

  • 请使用我的完整代码,看看它是否有效,我已经尝试过你的线路但它没有工作
  • @Mike 您能否提供在您的示例中工作的代码的小提琴或代码沙箱,我可以添加我的更改到那个
  • 您复制我的代码并将其放入检查->控制台它将运行...我尝试了您的代码但它不起作用,请更改我的完整代码,我也是新人所以我是不熟悉你提到的事情。
【解决方案2】:

只需将\s+(一个或多个字符的任何空格)替换为" "。如果这是您的目标,您也可以在同一个正则表达式上进行拆分。

document.addEventListener('select', () => {
  if (window.getSelection) {
    text = window.getSelection().toString();
    words = text.trim().replace(/\s+/g, " ");
    console.log(words);
  }
});
<textarea rows=4>
foo
bar
</textarea>

【讨论】:

    【解决方案3】:

    您可以将 \n 和 \r\n 替换为空格。取决于操作系统使用哪一个。 如果您不想打扰操作系统检查,请先替换 \r\n,然后替换 \n。

    更新:

    代码是:text = text_1.replace(/\r?\n/g, " ");

    【讨论】:

    • 你能不能把它放到上面的代码中并检查它是否有效?
    【解决方案4】:

    您可以替换新行和回车,然后替换空格。

    window.addEventListener(
      "keydown",
      function(e) {
        if (e.keyCode === 16) {
          var text = "";
          if (window.getSelection) {
            text_1 = window.getSelection().toString();
            text = text_1
                    .replace(/\n|\r/g, " ")
                    .replace(/\s/, "");
          }
            words = text.split(" ");
            console.log(words);
        }
      },
      false
    );
    

    【讨论】:

    • 我认为在替换之前使用trim会比在之后过滤更好的解决方案
    • @GammaGames 将代码更改为两个replaces。 trim 在这种情况下不起作用。