trouble with string replace

L

laredotornado

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
 
D

David Mark

laredotornado said:
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).
 
L

laredotornado

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.  ;)



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
 
D

David Mark

laredotornado said:
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!
 
T

Thomas 'PointedEars' Lahn

David said:
laredotornado said:
$('.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
 
J

John G Harris

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
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top