hluk / copyq-commands

Useful commands for CopyQ clipboard manager.
331 stars 73 forks source link

trying to make command to replace single quote with two single quotes and vice versa #14

Closed gooddadmike closed 5 years ago

gooddadmike commented 5 years ago

I'm doing a lot of database word where I'm passing some dynamic sql.

CASE WHEN [t].[VALUE] = 'Foo' THEN NULL ELSE 'Bar' END

and I often need to reverse it from '' to ' as well.

I have tried it this way

copyq:
function modifyText(text)
{
  // TODO: Modify text here!
  return text.replace("'", "''")
}

if (!copy())
  abort()

var text = str(clipboard())
var newText = modifyText(text)
if (text == newText)
  abort();

copy(newText)
paste()

and it only replaces the very first ' with ''. It never gets them both. Any help appreciated.

garulovilla commented 5 years ago

You need to use a regex text.replace(/'/g, "''"). If you don't use a regex only the first occurrence is replaced. Yo can check the documentation in String.prototype.replace().

gooddadmike commented 5 years ago

This solved me up. Thanks!