[texworks] Qtscripts help

Jonathan Kew jfkthame at googlemail.com
Fri Jan 15 21:02:12 CET 2010


On 15 Jan 2010, at 19:02, kap4lin wrote:

> This may not the be appropriate list to ask for Qtscript help, so my
> apologies ahead.
> 
> I am trying to replace the newline character by a space. Why does the
> following not work?
> 
> var txt = TW.target.selection;
> txt = txt.replace(/\n/g,' ');
> TW.target.insertText(txt);
> undefined;
> 
> I've also tried, txt.replace(/(.*)\n(.*)/g, "$1 $2"). Tried with \\n,
> \\r, \r\n, \\n\\r, .. all sensible combination! The replace(...) does
> not work inside the debugger itself!!


The reason is that internally (regardless of the line endings that were in the original file), the Qt editor represents newlines with the Unicode character U+2029 (BLOCK SEPARATOR). So you need to use

  txt.replace(/\u2029/g, ' ');

to achieve what you're trying to do here.

(BTW, with the new script APIs you no longer need to ensure you return "undefined" from a QtScript to avoid getting a result dialog; to actually return something, you'd have to explicitly assign to TW.result.)

JK




More information about the texworks mailing list