Skip to main content
Retsied
Known Participant
February 28, 2019
Answered

Script for detecting white spaces in text layer and inserting new line

  • February 28, 2019
  • 1 reply
  • 1128 views

I have several different psd files of various names, some with or without a space between words.

What I'm attempting to do is add a new text layer with text equal to the file name. I've found a script that accomplishes this (thanks Julieanne Kost).

However, if the file name contains multiple words with spaces (example: "Eat This Apple"), I would like to display the text as 3 separate lines, center-aligned.

Is there a way to accomplish this? Maybe by detecting white space in the given text layer, and inserting a new break line at every instance?

Similarly, looking for a way also remove spaces in a different case.

Thanks!

This topic has been closed for replies.
Correct answer r-bin

Try

text = text.replace(/\s+/g, " ");

text = text.replace(/\s/g, "\n");

where text - is your text string with spaces.

1 reply

r-binCorrect answer
Legend
February 28, 2019

Try

text = text.replace(/\s+/g, " ");

text = text.replace(/\s/g, "\n");

where text - is your text string with spaces.

Retsied
RetsiedAuthor
Known Participant
March 1, 2019

Thanks, this worked for removing the spaces.  I added a "\r" to the second line to add new line break.