So I'm pulling in text from a csv and I am trying to find a way to prevent widows (where there is a single word on a line).

in JS we'd do this text = text.replace(/\s(?=[^\s]*$)/g, ' ');

But I can't seem to find a similar method in After Effects with expressions, to insert a nonbreaking space between the last word and the second last word in a string.

1 Answer 1


There is not much information about this. I have found a discussion where someone said there could be a bug with regex lookahead, maybe it's not even implemented (source).

Instead, you could capture the last word and add it along with the space (captured elements start with index 1, so $1 is the first and in this case only group):

text = text.replace(/\s([^\s]*$)/g, " $1");
  • Only issue is that I can't seems to place a non-breaking space that AF respects and doesn't just treat as a regular space.
    – Picard102
    Commented Jul 9, 2021 at 3:09
  • used \u2007 to create the space. Only issue was that it started breaking words onto the next line. lol Can never win.
    – Picard102
    Commented Jul 9, 2021 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.