fvg
fvg

Reputation: 11

Problems with string manipulation in an UltraEdit script

Inside an UltraEdit script I've got a bit of code like so:

var FNstring = UltraEdit.activeDocument.selection;
var FNstring = UltraEdit.activeDocument.selection.replace(">>","</p>");
var FNstring = UltraEdit.activeDocument.selection.replace("\r\n","<BR>");

A var_dump confirms that the content of the string at this moment is:

text text text
text text text text text text text text text
text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text>>

I'm just not getting those find/replace commands right:

  1. Replace the >> with (/p) (once only)

  2. Replace all CR/LF with (br). (0 or >1 )

That code above is just not doing it. I'm getting something wrong. How can I fix this?

Upvotes: 1

Views: 623

Answers (1)

Mofi
Mofi

Reputation: 49097

Those three lines of code do the following:

  1. Create a copy of the selection in the active document in UltraEdit into a JavaScript string variable.
  2. Create a copy of the selection in active document in UltraEdit into a JavaScript string variable with the same name as before, resulting in discarding the previous string and additionally replacing >> by </p> in the created copy.
  3. Create a copy of the selection in active document in UltraEdit into a JavaScript string variable with the same name as before, resulting in discarding the previous string and additionally replacing \r\n by <BR> in the created copy.

That is not what you want. The replace function of the JavaScript String object has nothing to do with the replace function of the UltraEdit document object.

You could use instead following two lines:

var FNstring = UltraEdit.activeDocument.selection.replace(/>>/g,"</p>");
UltraEdit.activeDocument.write(FNstring.replace(/\r\n|\n|\r/g,"<BR>"));

Those two lines do following:

  1. Create a copy of the current selection in the active document of UltraEdit into the JavaScript string variable FNstring with replacing all occurrences of >> by </p> in the created copy.
  2. Create one more copy of the previously created string FNstring with replacing all occurrences of a DOS (Windows) or UNIX or Mac line termination by <BR> and write this new string over the selection in the active document of UltraEdit.

Upvotes: 1

Related Questions