trouble with string replace

Discussion in 'Javascript' started by laredotornado, Feb 22, 2010.

  1. 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
    laredotornado, Feb 22, 2010
    #1
    1. Advertising

  2. laredotornado

    David Mark Guest

    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).
    David Mark, Feb 22, 2010
    #2
    1. Advertising

  3. On Feb 22, 2:42 pm, David Mark <> 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
    laredotornado, Feb 22, 2010
    #3
  4. laredotornado

    David Mark Guest

    laredotornado wrote:
    > On Feb 22, 2:42 pm, David Mark <> 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!
    David Mark, Feb 22, 2010
    #4
  5. 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
    Thomas 'PointedEars' Lahn, Feb 22, 2010
    #5
  6. 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
    John G Harris, Feb 23, 2010
    #6
  7. 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.
    Richard Cornford, Feb 23, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    365
    Bruno Desthuilliers
    Jun 27, 2006
  2. Robert Kern
    Replies:
    2
    Views:
    489
    Neil Cerutti
    Feb 7, 2007
  3. Alun
    Replies:
    3
    Views:
    4,479
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    214
    Dr John Stockton
    Aug 27, 2004
  5. V S Rawat
    Replies:
    5
    Views:
    282
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page