Skip to main content
Retsied
Known Participant
July 21, 2019
Answered

Script to remove commas and apostrophes from text layer

  • July 21, 2019
  • 2 replies
  • 2397 views

I'm trying to simply remove all spaces, commas and apostrophes from a text layer. I've got the spaces out with the below code, however, doing something similar for the apostrophes doesn't seem to work. Any thoughts? Thanks!

myText = myText.replace(/ |,/g, "");

myText = myText.replace(/'|,/g, "");

This topic has been closed for replies.
Correct answer Chuck Uebele

r-bin's is close, but has issues with the apostrophes in a text layer, and I think this is due to PS using smart quotes, so you have to put in the keycode for smart quotes when doing the regular expression. This worked for me, but not sure if it will work if you copy and paste.

var doc = activeDocument

var lay = doc.activeLayer

lay.textItem.contents = lay.textItem.contents.replace(/(,|\s|'|‘|’|`)/g, "")

2 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
July 22, 2019

r-bin's is close, but has issues with the apostrophes in a text layer, and I think this is due to PS using smart quotes, so you have to put in the keycode for smart quotes when doing the regular expression. This worked for me, but not sure if it will work if you copy and paste.

var doc = activeDocument

var lay = doc.activeLayer

lay.textItem.contents = lay.textItem.contents.replace(/(,|\s|'|‘|’|`)/g, "")

Stephen Marsh
Community Expert
Community Expert
July 22, 2019

After r-bin's hint that apostrophe and single quotes may be the issue, I started to look for specifying the regex character via it's unicode value... However I don't even know if Adobe's regex implementation supports them.

Chuck Uebele
Community Expert
Community Expert
July 22, 2019

On Windows, I typed in the alt code: alt-0145 and alt-0146, and that seemed to work.

Stephen Marsh
Community Expert
Community Expert
July 21, 2019

Regular expression metacharacters need to be escaped with a backslash. You should be able to use all three in a single replace:

(/\,| |\'/g, "")

or

(/\,|\s|\'/g, "")

Retsied
RetsiedAuthor
Known Participant
July 21, 2019

Hmm, this didn't seem to work with replacing the apostrophe

Stephen Marsh
Community Expert
Community Expert
July 21, 2019

Apologies, I was escape happy!

Try removing the backslash escape character from the apostrophe, it is a literal character and not a metacharacter (however in a regex tester it works both ways, I did not test in a scripting environment).