user35387: 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, '&nbsp;');
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.

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


35387: <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>
 29988: <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>
