Click to See Complete Forum and Search --> : newline character


freeman55
12-03-2003, 05:36 PM
this message is sent out because i couldnt find any clear answer to my request on the web, and i need a clear answer because my javascript skills are nearly null.

I am trying to integrate a javascript based rich text editor to a JavaServer Page (JSP). the editor is opensource and from: http://www.kevinroth.com/rte/demo.htm (http://).

the editor has a function which i can use to set a text inside the editor at startup.

writeRichText('rte1', '<%= paragraph.getText() %>', 560, 200, true);

the second parameter is a JSP declaration that returns a JAVA string object that contains the text that the editor should be initialized with.

the problem is:
Error: unterminated string literal
Source File: http://********
Line: 77, Column: 24
Source Code:
writeRichText('rte1', '<P>asdfasdfsadfs</P>

this occurs beacause the returned string object contains a newline character.

my question is:
how do i replace newline characters to html's <br> and what a newline character looks like? any java or javascript code example will be very wellcome.
thx;)

TheBearMay
12-03-2003, 07:00 PM
Since the newline character is the last character of the string you might try using the text.substring function to select all but the last character, ie text.substring(0,text.length-1).

freeman55
12-04-2003, 08:55 AM
thanks for your feedback.
There is though an important point i forgot to mention: the returned java String object contains multiple new lines. as you can see paragraph.getText() returns the text of a complete paragraph object which might contain several newline characters.

ray326
12-04-2003, 10:32 AM
Here's an example of how you can replace newlines with spaces in a string but you'll have to figure out how to apply this because your example is vbscript, no javascript.

var a = "now is\nthe time\nto come\n".match(/([^\n]+)/g);
var b = a.join(" ");

fredmv
12-04-2003, 10:50 AM
Originally posted by ray326
var a = "now is\nthe time\nto come\n".match(/([^\n]+)/g);Why not:var a = "now is\nthe time\nto come\n".replace(/\n/g, " ");Just wondering.

ray326
12-04-2003, 11:02 AM
Even better. I'm always just matching stuff; didn't even realize there was a replace method like that.

freeman55
12-05-2003, 09:19 AM
thanks to fred and ray... this is a neat way to resolve the problem... i figured out that \n is the symbol for a newline character, but does this /g do? i am not sure of what it is.

keep the preasure! :)

freeman55
12-07-2003, 11:19 AM
heh,
I found out that replace(/\n/g, " ") means replace all \n with a spaceline. with /x/g being an added modifier that trigger a repeat replacement. The solution works fine but there is still a mysterious problem:

i replace all \n character with "test" but still the newline is in the output!?
the function somehow only adds the word test at every newline character... see the code data below:

input (with 2 newline characters marked as *):
<p><b>paragraph title</b></p>*
<p>paragraph text</p>*
<p>second paragraph</p>

the code:

<%String all = paragraph.getText().replaceAll("\n", "test"); %>
<td colspan="3">
<script language="JavaScript" type="text/javascript">
<%= all %>
</script>
</td>


Output:
<td colspan="3">
<script language="JavaScript" type="text/javascript">
<p><b>paragraph title</b></p>
test<p>paragraph text</p>
test<p>second paragraph</p>
</script>
</td>

i am using j2sdk 1.4.1's method replaceAll instead of the g modifier... as you can see the output still contains newlines!!
ps. : <%= all %> is a jsp declaration that roughly does the same as document.writeln()

ray326
12-07-2003, 12:04 PM
Try it like this instead. You're mixing server side Java with client side Javascript and they can't see each other.

<%String all = paragraph.getText().replaceAll("\n", "test"); %>
<td colspan="3">
<%= all %>
</td>

freeman55
12-09-2003, 03:26 PM
my problem is solved. I had to replace both "\r", carraige returns, and "\n" characters. :) in the string.

<%String all = paragraph.getText().replaceAll("\r", "CR").replaceAll("\n", "LF"); %>

thanks to all