如何修改选择到前一个和后一个空格或换行符。

huangapple go评论61阅读模式
英文:

How to modify selection to the previous and next space or new line

问题

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
$ ('button'). on ('click', function () {
    const selection = window.getSelection();
    selection?.modify('move','backward','word');
    selection?.modify('extend','forward','word');
    console.log(selection.toString());
});

<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<pre contenteditable>
https://www.youtube.com/watch?v=vEQ8CXFWLZU
https://www.youtube.com/watch?v=vEQ8CXFWLZU
lorem ipsum https://www.youtube.com/watch?v=vEQ8CXFWLZU
https://www.youtube.com/watch?v=vEQ8CXFWLZU lorem ipsum
https://www.youtube.com/watch?v=vEQ8CXFWLZU lorem ipsum
</pre>
<button>点击</button>

<!-- 结束代码片段 -->

将光标放在YouTube URL内
单击按钮后,我需要在控制台中获取整个URL,而不仅仅是一部分
换句话说,我需要像这样的东西:

selection?.modify('move','backward','到前一个空格或新行');
selection?.modify('extend','forward','到下一个空格或新行');
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;button&#39;).on(&#39;click&#39;, function(){
const selection = window.getSelection();
selection?.modify(&#39;move&#39;, &#39;backward&#39;, &#39;word&#39;);
selection?.modify(&#39;extend&#39;, &#39;forward&#39;, &#39;word&#39;);
console.log(selection.toString());
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;pre contenteditable&gt;
lorem ipsum https://www.youtube.com/watch?v=vEQ8CXFWLZU https://www.youtube.com/watch?v=vEQ8CXFWLZU lorem ipsum https://www.youtube.com/watch?v=vEQ8CXFWLZU lorem ipsum &lt;/pre&gt; &lt;button&gt;CLICK&lt;/button&gt;

<!-- end snippet -->

Place the cursor inside a youtube url
Clicking on a button I need the entire url in console, and not just a part
In other words I need something like this:

selection?.modify(&#39;move&#39;, &#39;backward&#39;, &#39;to the previous space or new line&#39;);
selection?.modify(&#39;extend&#39;, &#39;forward&#39;, &#39;to the next space or new line&#39;);

答案1

得分: 2

使用正则表达式,您可以设置您的规则。

$('button').on('click', function() {
  const selection = window.getSelection();

  let [bws, aws] = [false, false];

  let [
    [bn, bo],
    [an, ao]
  ] = [
    [selection.anchorNode, selection.anchorOffset],
    [selection.focusNode, selection.focusOffset]
  ]
  .sort(function(aa, bb) {
    return aa[1] - bb[1];
  });

  while (!bws && 0 < bo) {
    selection.setBaseAndExtent(bn, --bo, an, ao);

    if ((bws = (-1 !== selection.toString().search(/\r?\n| /)))) {
      ++bo;
    }
  }

  while (!aws && an.length >= ao + 1) {
    selection.setBaseAndExtent(bn, bo, an, ++ao);
    if ((aws = (-1 !== selection.toString().search(/\r?\n| /)))) {
      --ao;
    }
  }
  selection.setBaseAndExtent(bn, bo, an, ao);
  console.log(selection.toString());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<pre contenteditable>
https://www.youtube.com/watch?v=vEQ8CXFWLZU
 https://www.youtube.com/watch?v=vEQ8CXFWLZU
lorem ipsum https://www.youtube.com/watch?v=vEQ8CXFWLZU
https://www.youtube.com/watch?v=vEQ8CXFWLZU lorem ipsum
 https://www.youtube.com/watch?v=vEQ8CXFWLZU lorem ipsum
</pre>
<button>CLICK</button>
英文:

With the help of Regex you can set your rule.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;button&#39;).on(&#39;click&#39;, function() {
  const selection = window.getSelection();

  let [bws, aws] = [false, false];

  let [
    [bn, bo],
    [an, ao]
  ] = [
    [selection.anchorNode, selection.anchorOffset],
    [selection.focusNode, selection.focusOffset]
  ]
  .sort(function(aa, bb) {
    return aa[1] - bb[1];
  });

  while (!bws &amp;&amp; 0 &lt; bo) {
    selection.setBaseAndExtent(bn, --bo, an, ao);

    if ((bws = (-1 !== selection.toString().search(/\r?\n| /)))) {
      ++bo;
    }
  }


  while (!aws &amp;&amp; an.length &gt;= ao + 1) {
    selection.setBaseAndExtent(bn, bo, an, ++ao);
    if ((aws = (-1 !== selection.toString().search(/\r?\n| /)))) {
      --ao;
    }
  }
  selection.setBaseAndExtent(bn, bo, an, ao);
  console.log(selection.toString());
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;pre contenteditable&gt;
lorem ipsum https://www.youtube.com/watch?v=vEQ8CXFWLZU https://www.youtube.com/watch?v=vEQ8CXFWLZU lorem ipsum https://www.youtube.com/watch?v=vEQ8CXFWLZU lorem ipsum &lt;/pre&gt; &lt;button&gt;CLICK&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月15日 11:52:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定