XHR responseText containing literal newlines (\n).

L

-Lost

For example:

var newlines = 'a\n\nb\n\nc';
alert(newlines);

Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:

a\n\nb\n\nc

....not...
a
b
c

Anyone know offhand if it is possible to get the alert to use the \n's
found in a responseText?
 
C

Cah Sableng

For example:

var newlines = 'a\n\nb\n\nc';
alert(newlines);

Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:

a\n\nb\n\nc

...not...
a
b
c

Anyone know offhand if it is possible to get the alert to use the \n's
found in a responseText?

I'm not clearly understand what exactly you want.
Which one do you need?

function ex(xhrObj)
{
if (/\\n/mg.test(xhrObj.responseText))
alert('newline');

alert(xhrObj.responseText.replace(/\\n/mg,"\n"));
}

HTH
 
L

-Lost

Cah said:
I'm not clearly understand what exactly you want.
Which one do you need?

function ex(xhrObj)
{
if (/\\n/mg.test(xhrObj.responseText))
alert('newline');

alert(xhrObj.responseText.replace(/\\n/mg,"\n"));
}

Thanks, that works. (The replacement not the test.)

Maybe I am missing something fundamental, but replacing \n with \n seems
really, really funny to me.
 
R

rf

-Lost said:
For example:

var newlines = 'a\n\nb\n\nc';

'a\n\n\b\n\n\c' above contains the letter 'a' then the character '\' then
the letter 'n' then '\' then 'n' then 'b'

The value of the variable newlines above is, after parsing of 'a\n\nb\n\nc'
by the interpreter and assigning the result to the variable, the letter 'a'
then a newline then newline then the letter 'b' then...
alert(newlines);

Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:

a\n\nb\n\nc

It's not the "exact same line".

What you get in the response text is the string 'a\n\n\b\n\nc'.

What you get up the top there is this string parsed by the javascript
interpreter who carefully translates the character sequence \n to a newline
character.

BTW xhrObj.responseText.replace(/\\n/mg,"\n"); is not translating \n into
\n, or even a newline into a newline. It is searching for the two character
string, \n, with the \ escaped of course, so we write it /\\n/. It is
replacing occurrences of this string with something, the something being the
string "\n" which has of course been parsed by the interpreter into a string
containing the single character 'newline'.
 
C

Cah Sableng

'a\n\n\b\n\n\c' above contains the letter 'a' then the character '\' then
the letter 'n' then '\' then 'n' then 'b'

The value of the variable newlines above is, after parsing of 'a\n\nb\n\nc'
by the interpreter and assigning the result to the variable, the letter 'a'
then a newline then newline then the letter 'b' then...




It's not the "exact same line".

What you get in the response text is the string 'a\n\n\b\n\nc'.

What you get up the top there is this string parsed by the javascript
interpreter who carefully translates the character sequence \n to a newline
character.

BTW xhrObj.responseText.replace(/\\n/mg,"\n"); is not translating \n into
\n, or even a newline into a newline. It is searching for the two character
string, \n, with the \ escaped of course, so we write it /\\n/. It is
replacing occurrences of this string with something, the something being the
string "\n" which has of course been parsed by the interpreter into a string
containing the single character 'newline'.

Umm..
I think I saw string variable enclosed inside single quotes before
replied.. :)
But your last paragraph is right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top