Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   trouble with string replace (http://www.velocityreviews.com/forums/t940515-trouble-with-string-replace.html)

laredotornado 02-22-2010 09:27 PM

trouble with string replace
 
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

David Mark 02-22-2010 09:42 PM

Re: trouble with string replace
 
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_not...ml#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/Cor...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).

laredotornado 02-22-2010 10:54 PM

Re: trouble with string replace
 
On Feb 22, 2:42*pm, David Mark <dmark.cins...@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_not...ml#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/Cor...ference/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


David Mark 02-22-2010 10:57 PM

Re: trouble with string replace
 
laredotornado wrote:
> On Feb 22, 2:42 pm, David Mark <dmark.cins...@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_not...ml#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/Cor...ference/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!

Thomas 'PointedEars' Lahn 02-22-2010 11:27 PM

Re: trouble with string replace
 
David Mark wrote:

> laredotornado wrote:
>> $('.replaceLineBreaks').each(function() {
>> console.log("replacing " + $(this).html());
>> $(this).html( $(this).html().replace('/\\n/g', '<BR/>') );
>> });
>>
>> Any ideas how to fix 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).


It would be invalid markup anyway. XHTML element type names are
case-sensitive, and it is the `br' element in XHTML.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

John G Harris 02-23-2010 04:46 PM

Re: trouble with string replace
 
On Mon, 22 Feb 2010 at 22:45:15, in comp.lang.javascript, Richard
Cornford wrote:
>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),

<snip>

Note that the Draft HTML 5 spec. allows /> .

John
--
John Harris

Richard Cornford 02-23-2010 05:07 PM

Re: trouble with string replace
 
On Feb 23, 4:46*pm, John G Harriswrote:
> On Mon, 22 Feb 2010 at 22:45:15, RichardCornford wrote:
>>laredotornado wrote:

<SNIP>
>>> "\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),

>
> * <snip>
>
> Note that the Draft HTML 5 spec. allows /> .


Do you remember an ES4 draft?

Richard.


All times are GMT. The time now is 11:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57