简体   繁体   中英

How can I copy to clipboard specific part of paragraph?

I want to make the method or only copy to clipboard the "syntax" part of a paragraph. I've done the logic to get the specific part of content I want and stored it in variable "syntaxClean". Now I just need to copy it somehow.

document.execCommand("copy"); would be awesome, but I just can't seem to make it work.

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Test</title> </head> <body> <pre id="test"></pre> <script> const message = "====== Executor details =======\\nathena@21.109.21.25 (tunneled:39516)\\n====== Request details ========\\nClass....................: com.ericsson.athena.taf.os.linux.commands.common.SimpleCmdRequest\\nThread : ................: main\\nExpected prompt..........: ^((?![&lt;?]|\\\\\\\\.{3}|(\\\\\\\\S+\\\\\\\\s){6,}).)*[&gt;#$%]+(\\\\\\\\\\\\\\\\\\\\\\\[(\\\\\\\\d;?)*[m|n|K])*\\\\\\\\s(\\\\\\\\\\\\\\\\\\\\\\\[(\\\\\\\\d;?)*[m|n|K])*$|#\\\\\\\\s\\\\\\\\\\\\\\\[6n\\nPrompt forced............: false\\nTimeout..................: 20000ms\\nSyntax...................: lsb_release -i\\n" document.getElementById("test").append(message); var res = message.split("\\n"); for (var i in res) { if (res[i].indexOf("Syntax") != -1) { var syntax = res[i].split(':'); var syntaxClean = syntax[1].slice(1); console.log(syntaxClean); } } </script> </body> </html> 

In this example I would like to copy to clipboard "lsb_release -i" and I have it stored in variable syntaxClean as I've already said above.

Any help is appreciated!

You can achieve this by creating a dummy textarea like this:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Test</title> </head> <body> <pre id="test"></pre> <button onclick="copy()">Copy</button> <script> const message = "====== Executor details =======\\nathena@21.109.21.25 (tunneled:39516)\\n====== Request details ========\\nClass....................: com.ericsson.athena.taf.os.linux.commands.common.SimpleCmdRequest\\nThread : ................: main\\nExpected prompt..........: ^((?![&lt;?]|\\\\\\\\.{3}|(\\\\\\\\S+\\\\\\\\s){6,}).)*[&gt;#$%]+(\\\\\\\\\\\\\\\\\\\\\\\[(\\\\\\\\d;?)*[m|n|K])*\\\\\\\\s(\\\\\\\\\\\\\\\\\\\\\\\[(\\\\\\\\d;?)*[m|n|K])*$|#\\\\\\\\s\\\\\\\\\\\\\\\[6n\\nPrompt forced............: false\\nTimeout..................: 20000ms\\nSyntax...................: lsb_release -i\\n" document.getElementById("test").append(message); function copy() { var res = message.split("\\n"); for (var i in res) { if (res[i].indexOf("Syntax") != -1) { var syntax = res[i].split(':'); var syntaxClean = syntax[1].slice(1); console.log(syntaxClean); copyToClipboard(syntaxClean); } } } function copyToClipboard(text) { var dummyElm = document.createElement("textarea"); document.body.appendChild(dummyElm); dummyElm.value = text; dummyElm.select(); document.execCommand("copy"); document.body.removeChild(dummyElm); } </script> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM