Remove Special Characters From Copied Clipboard Text

I want to remove all the special characters from copied text before pasting it. Can someone please help?

What do you mean by "special characters"?

!	"	#	$	%	&	'	*	+		‑		/ are the special characters.

OK. A Search and Replace action using a simple regular expression will do the job. Here's an example:

Test Remove Special Characters.kmmacros (2.9 KB)

Click to see image of macro

Keyboard Maestro Export

not working for me, I have copied this text " demo&#*&&#**_text " , and then run your macro, but it is not showing "demotext " in window.

It's not obvious from your list that you also wanted spaces to be removed -- there's a whole bunch of other potential special characters too, depending on your source eg "é".

Have you a complete list -- or perhaps more usefully, a list of characters that shouldn't be removed?

Do you mean copied to clipboard? If so, that's not how my example macro works. If you take the time to actually read the macro you will see that it takes the text in the variable LocalTestVar and processes that.

If you need to work with the clipboard, then simply change the Search and Replace action to use the clipboard and not the variable like this:

KM 0 2022-04-29_11-39-41

Still not working for me. Sorry but I can barely understand your macro. I am a newbie. I have copied the text "demo&#&&#**_ text" , then ran your macro. then I paste it. It was pasted "demo&#&&#**_ text"

kindly see the attached macro
Remove Special Characters.kmmacros (3.1 KB)

OK. I've changed your macro to how I think it should be but I suspect there's another problem.

Here's your modified macro:

Remove Special Characters.kmmacros (1.7 KB)

Click to see macro

Keyboard Maestro Export

The problem is to do with the hot key trigger you have chosen Up Arrow

I think it's a problem because while testing the macro the text editor I'm using (Sublime) actually captures that hot key combination and therefore stops the macro from ever firing and the result looks like this:

KM 1 2022-04-29_12-07-04
You can see that the hot key selects all of the text while moving the cursor to the top.

When I triggered the macro manually this is what I got:

KM 2 2022-04-29_12-08-37

so you can see that macro does work but you’ll need to use a different hot key perhaps.

Hi @hiren4776 - I've been doing some more testing and found this version of your macro seems to work in several editors without a problem. All you have to do is first select the text you want to process and then use the hot key to trigger the macro. The macro will then copy the selected text, remove the special characters and finally paste it back over what you originally selected. Try it and see if it works for you!

Remove Special Characters (Revised).kmmacros (2.8 KB)

Click to see macro

Keyboard Maestro Export

1 Like

Hi @tiffle,

Your script is running good, but I have a problem.
Try this below text with the macro I have attached here:
spc char remove.kmmacros (2.4 KB)

Input:

A'spr 27, 2022 11:01 AM Pacific Time (US and Canada) ID: 834 009 8553

Output:

Aspr 27 2022 1101 AM Pacific Time (US and Canada) ID 834 009 8553

Why is it still not removing () ?

Because they weren't in your list of "characters to be removed".

In that regular expression in the macro @tiffle wrote for you, there's a list of characters between the "[" and "]". What that action does is:

"For each character on the clipboard, if it matches any of the characters in the list then replace it with 'nothing' (an empty text string)" -- so it deletes any character that matches.

If you want more characters removing, add them to the list between "[" and "]" .

2 Likes