From: laredotornado on
Hi,

I'm using the JQuery framework but this question is about the string
replace method. I'm trying to replace carriage returns "\n" with the
"<BR/>" tag. Sadly, the replace method I have below is not
working ...

$('.replaceLineBreaks').each(function() {
console.log("replacing " + $(this).html());
$(this).html( $(this).html().replace('/\\n/g', '<BR/>') );
});

Any ideas how to fix it? Thanks, - Dave
From: David Mark on
laredotornado wrote:
> Hi,
>
> I'm using the JQuery framework but this question is about the string
> replace method.

Unfortunately, one of the side effects of using such a "framework" is
that it makes it impossible to get help from those who do not use it.
And, of course, the quality of advice available from those who rely on
jQuery to (sort of) do their work for them is typically abysmal as they
are shielded from learning anything about the language they are trying
to use. ;)

> I'm trying to replace carriage returns "\n" with the
> "<BR/>" tag. Sadly, the replace method I have below is not
> working ...

http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork

>
> $('.replaceLineBreaks').each(function() {
> console.log("replacing " + $(this).html());
> $(this).html( $(this).html().replace('/\\n/g', '<BR/>') );
> });
>
> Any ideas how to fix it?

You are (sort of) in luck as at least one mistake is obvious (the quotes
around the "regular expression", which makes it a string).

https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/Replace

Note that the first argument can be either a string or a regular
expression. Your example uses some sort of strange hybrid. Think about it.

Also, jQuery doesn't work at all in XHTML DOM's, so the BR doesn't need
a slash (in fact, it will just be thrown out by the browser).
From: Richard Cornford on
laredotornado wrote:
> Hi,
>
> I'm using the JQuery framework but this question is about the
> string replace method. I'm trying to replace carriage returns
> "\n" with the "<BR/>" tag.

That is a very strange tag. If it is SGML then the slash at the end
changes the meaning in an unhelpful way, if it is HTML then the slash at
the end is an error (that browsers can automatically correct, at the
cost of a small overhead), and if it is XHTML (which it isn't as JQuery
does not support XHTML DOMs) then the "BR" should be lowercase in
case-sensitive XHTML. Generally, you should decide (or know/understand)
which type of document you are working with and write (more or less
valid) mark-up that is appropriate for that type of document.

> Sadly, the replace method I have below is not
> working ...
>
> $('.replaceLineBreaks').each(function() {
> console.log("replacing " + $(this).html());
> $(this).html( $(this).html().replace('/\\n/g', '<BR/>') );
> });
>
> Any ideas how to fix it? Thanks, - Dave

Well, first of the first argument you are providing for - replace - is a
string literal, when it looks like what you wanted was a regular
expression literal. Although - replace - will convert string arguments
into regular expression literal it will use the string as the pattern
that will be matched, so will be trying to match something that ends -
/g -, which isn't going to be a carriage return. Just removing the
apostrophise should make the regular expression 'correct'.

However, the value you are searching for the carriage returns is
effectively an - innerHTML - value (that is what JQuery's - html -
method retrieves internally). The values returned by - innerHTML - are a
serialisation of the (appropriate branch of) the DOM, and DOMs represent
as post HTML parsing interpretation of a document. In HTML whitespace
characters, including carriage returns, are 'normalised' based on
context. Series of whitespace characters can act as a single white space
character (in text, for example) or thay can be ignored completely (such
as between an opening TR tag and an opening TD tag). There is no
requirement for the non-significant whitespace to be represented in the
DOM (indeed the specs leave the decision about whether they should be
represented entirely up to the individual implementation), and so there
is no reason to expect to see that whitespace in a serialisation of the
DOM. It may well turn out to be the case that in some environments these
carriage returns that you re expecting are just no longer there in the
DOM, will not show up in the - innerHTML - value, and so will never be
replaced by any means.

Richard.

From: laredotornado on
On Feb 22, 2:42 pm, David Mark <dmark.cins...(a)gmail.com> wrote:
> laredotornado wrote:
> > Hi,
>
> > I'm using the JQuery framework but this question is about the string
> > replace method.
>
> Unfortunately, one of the side effects of using such a "framework" is
> that it makes it impossible to get help from those who do not use it.
> And, of course, the quality of advice available from those who rely on
> jQuery to (sort of) do their work for them is typically abysmal as they
> are shielded from learning anything about the language they are trying
> to use.  ;)
>
> > I'm trying to replace carriage returns "\n" with the
> > "<BR/>" tag.  Sadly, the replace method I have below is not
> > working ...
>
> http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork
>
>
>
> >            $('.replaceLineBreaks').each(function() {
> >                    console.log("replacing " + $(this).html());
> >                    $(this).html( $(this).html().replace('/\\n/g', '<BR/>') );
> >            });
>
> > Any ideas how to fix it?
>
> You are (sort of) in luck as at least one mistake is obvious (the quotes
> around the "regular expression", which makes it a string).
>
> https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Object...
>
> Note that the first argument can be either a string or a regular
> expression.  Your example uses some sort of strange hybrid.  Think about it.
>
> Also, jQuery doesn't work at all in XHTML DOM's, so the BR doesn't need
> a slash (in fact, it will just be thrown out by the browser).

Changing the first argument to /\n/g solved the problem. JQuery
doesn't work in XHTML DOMs? Hmmm, I must be misunderstanding you as
that sounds like quite a gaping hole in JQuery. well, onto the next
issue, - Dave

From: David Mark on
laredotornado wrote:
> On Feb 22, 2:42 pm, David Mark <dmark.cins...(a)gmail.com> wrote:
>> laredotornado wrote:
>>> Hi,
>>> I'm using the JQuery framework but this question is about the string
>>> replace method.
>> Unfortunately, one of the side effects of using such a "framework" is
>> that it makes it impossible to get help from those who do not use it.
>> And, of course, the quality of advice available from those who rely on
>> jQuery to (sort of) do their work for them is typically abysmal as they
>> are shielded from learning anything about the language they are trying
>> to use. ;)
>>
>>> I'm trying to replace carriage returns "\n" with the
>>> "<BR/>" tag. Sadly, the replace method I have below is not
>>> working ...
>> http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork
>>
>>
>>
>>> $('.replaceLineBreaks').each(function() {
>>> console.log("replacing " + $(this).html());
>>> $(this).html( $(this).html().replace('/\\n/g', '<BR/>') );
>>> });
>>> Any ideas how to fix it?
>> You are (sort of) in luck as at least one mistake is obvious (the quotes
>> around the "regular expression", which makes it a string).
>>
>> https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Object...
>>
>> Note that the first argument can be either a string or a regular
>> expression. Your example uses some sort of strange hybrid. Think about it.
>>
>> Also, jQuery doesn't work at all in XHTML DOM's, so the BR doesn't need
>> a slash (in fact, it will just be thrown out by the browser).
>
> Changing the first argument to /\n/g solved the problem.

Not exactly, unless you are dealing exclusively with PRE elements (see
Richard's follow-up).

> JQuery
> doesn't work in XHTML DOMs?

Not a chance in hell. :)

Neither do any of the others, save for mine, which I consider to be a
mistake at this point. But the difference is that the others don't know
that they don't support it. Reading their code comments is quite
illuminating on this front.

As a matter of fact, when I opined in a jQuery forum that Resig had
never tested an XHTML DOM, I was unceremoniously "banned" from the
group. That's the mindset. :(

> Hmmm, I must be misunderstanding you as
> that sounds like quite a gaping hole in JQuery.

Not really. But it is a gaping hole in their understanding of Web
technologies. ;)

> well, onto the next

Onward and upward!