前提・実現したいこと
下記サイト様で見つけたソースが近いのですが、カッコ内(『』)の文字列だけをコピーしたいです。
ラボラジアン
「JavaScriptで画面上の文字列をクリップボードにコピーする方法」
参照URL:https://laboradian.com/copy-text-into-clipboard-by-js/
よろしくお願いいたします。
該当のソースコード
<script> document.querySelector('#btnCopy').addEventListener("click", () => { const element = document.querySelector('#item-name'), selection = window.getSelection(), range = document.createRange(); range.selectNodeContents(element); selection.removeAllRanges(); selection.addRange(range); //console.log('選択された文字列: ', selection.toString()); const succeeded = document.execCommand('copy'); if (succeeded) { alert('コピーが成功しました!'); } else { alert('コピーが失敗しました!'); } selection.removeAllRanges(); }); </script> <p>下のボタンを押すと、以下の文字列がクリップボードにコピーされます!</p> <button id="btnCopy">copy</button> <hr> <div id="item-name"> <table> <tbody> <tr><td>あああああああ『ほげほげ』あああああああ</td></tr> </tbody> </table> </div>すみません、修正しました。