<p>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 (<a href="https://community.adobe.com/t5/after-effects/regexp-bug/td-p/8904775" rel="nofollow noreferrer">source</a>).</p>
<p>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):</p>
<pre><code>text = text.replace(/\s([^\s]*$)/g, &quot; $1&quot;);
</code></pre>

CC BY-SA 4.0
2021-07-09T03:09:31.167Z
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
CC BY-SA 4.0
2021-07-09T03:25:21.373Z
used \u2007 to create the space. Only issue was that it started breaking words onto the next line. lol Can never win.
Picard102
CC BY-SA 4.0
2021-07-08T20:11:02.140Z
2021-07-08T20:11:02.140Z
Matt
<p>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).</p>
<p>in JS we'd do this <code>text = text.replace(/\s(?=[^\s]*$)/g, '&amp;nbsp;');</code></p>
<p>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.</p>

<after-effects><after-effects-expressions>
After Effects Prevent Widows in Text
Picard102
# After Effects Prevent Widows in Text

(Asked by: username_0 on 2021-07-08)

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.

**Tags:** after-effects, after-effects-expressions


---

## 1 answers

### Answer by username_1 on 2021-07-08. Score: 2 | Accepted answer

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");

> **Comments:**
> **username_0**: 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.  
> **username_0**: used \u2007 to create the space. Only issue was that it started breaking words onto the next line. lol Can never win.  

---

