Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-09T03:09:31.167Z
Answers.Text: 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.
Answers.UserDisplayName: Picard102
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-09T03:25:21.373Z
Answers.Text: used \u2007 to create the space. Only issue was that it started breaking words onto the next line. lol Can never win.
Answers.UserDisplayName: Picard102
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-08T20:11:02.140Z
Answers.LastActivityDate: 2021-07-08T20:11:02.140Z
Answers.UserDisplayName: Matt
Body: <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>
  
Tags: <after-effects><after-effects-expressions>
Title: After Effects Prevent Widows in Text
UserDisplayName: Picard102
ThreadText: # 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, '&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.
  
  **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](https://community.adobe.com/t5/after-effects/regexp-bug/td-p/8904775)).
  
  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.  
  
  ---
  
  