A literal answer to the question would be that you could use a non-breaking space after each preposition, whether with script or with "h1, h2 { prince-text-replace: 'with ' 'with\a0'; }".
However, I suggest that you don't necessarily want to force prepositions never to occur at the end of a line: in a title like
This is a title
among counter-examples
the preposition might well be better at the end of the preceding line.
You can get Prince for Books to make this decision (using "mechanical" criteria such as line length) by using markup such as
<style>.phrase { prince-wrap-inside: avoid; }</style>
<h1>This is a title <span class="phrase">among counter-examples</span></h1>
where prince-wrap-inside:avoid is a less forceful alternative to &#nbsp; or white-space:nowrap. For headings (or other non-justified text), it's best used with a recent build and combined with prince-line-break-choices: heading-lookahead (or body-lookahead for body text).
(See also
https://www.princexml.com/doc/11/properties/prince-wrap-inside/,
https://www.princexml.com/doc-prince/#pfb-princewrapinside.)
This less forceful alternative will tend to keep the associated text unbroken when the result looks reasonable in terms of line lengths (as in the "This is a title / with two lines" example) while allowing breaking in cases like the "... among / counter-examples" case.
However, even this isn't always what one wants, because the choice can be a matter of author intent: sometimes one wants a connective to be at the end of a line in order to give more prominence to what follows it. A book on learning to drive in Northern Africa might have a section related to the Citroën Sahara entitled
Changing Gears with
Two Engines
because the startling notion of a car with two engines is the most important part of the title. Whereas if the same title occurred in a book about the Citroën Sahara, then the important thing is "Changing Gears", and "with" would best go to the next line.
So, while one could write script to wrap each preposition and following word in a span and style it with prince-wrap-inside:avoid (or to force it by using prince-text-replace and a non-breaking space as mentioned earlier), you might be better off marking up phrases manually with human thought as to what's wanted.