0

I'm trying to use replace on a formatted text and to place the result in the clipboard. But .replace() works only with string. I have to replace hyperlinked links in clipboard. I'm trying to learn javascript but it's hard.

var text = window.getSelection();
var modified=text.replace("https://google", "google");
e.clipboardData.setData('text/plain', modified);

I checked .setData ad it allows only text not formatted string.

  • 1
    Try `"text/html"` format. – kol Aug 21 '21 at 08:31
  • It works thanks, but I don't get the replace to work. – hignirilti Aug 22 '21 at 18:30
  • `window.getSelection()` returns a `Selection` object, not a string. Calling `.replace` on it converts it to a simple string first, composed of the `textContent` of selected nodes, not their `innerHTML` or `outerHTML`. Have a look at how the `Selection` object works: https://developer.mozilla.org/en-US/docs/Web/API/Selection It seems possible to extract the selected elements from it along with their HTML content, if you'd like to replace parts of the HTML before copying it to the clipboard. – kol Aug 22 '21 at 19:47
  • Here is how to get the selected HTML: https://stackoverflow.com/a/4177234/600135 – kol Aug 22 '21 at 19:50

0 Answers0