replace all newlines with <br> tags

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lkrubner@geocities.com

    replace all newlines with <br> tags

    PHP has a function called nl2br() which takes all the newlines in a
    string and turns them into break tags <br>. I need to do the same in
    javascript. Is this about right? I'm not sure how one is supposed to
    reference invisible characters in Javascript strings. I took an example
    on the web and tried to modify it to my uses.

    function nl2br_js(myStri ng) {
    var regX = /\n/gi ;

    s = new String(myString );
    s = s.replace(regX, "<br /> \n");
    return s;
    }

    After looking around quite a bit using Google, I still couldn't find
    out what the gi in the above example is for. What is it?

  • Ivo

    #2
    Re: replace all newlines with &lt;br&gt; tags

    <lkrubner@geoci ties.com> wrote[color=blue]
    > PHP has a function called nl2br() which takes all the newlines in a
    > string and turns them into break tags <br>. I need to do the same in
    > javascript. Is this about right?
    >
    > function nl2br_js(myStri ng) {
    > var regX = /\n/gi ;
    >
    > s = new String(myString );
    > s = s.replace(regX, "<br /> \n");
    > return s;
    > }
    > After looking around quite a bit using Google, I still couldn't find
    > out what the gi in the above example is for. What is it?[/color]

    The 'g' flag makes the expression global so it finds all matches rather than
    just the first one.
    The 'i' flag makes it case-insensitive so that "a" matches "a" as well as
    "A". In your case, the are no letters to match so you can drop the 'i'.
    There is a global variable "s" in your function that serves no purpose; also
    the variable "regX" is not really needed. Try this cleaned up version:

    function nl2br_js(myStri ng){
    return myString.replac e( /\n/g, '<br />\n' );
    }

    hth
    --
    Ivo


    Comment

    • lkrubner@geocities.com

      #3
      Re: replace all newlines with &lt;br&gt; tags

      Thanks much. This gives me an error "Unterminat ed regular expression
      literal"; I'm sorry to stay I don't know enough about Javascript or
      regular expressions to trouble shoot this.

      Comment

      • lkrubner@geocities.com

        #4
        Re: replace all newlines with &lt;br&gt; tags

        I rewrite it like this:


        function nl2br_js(myStri ng){
        var regX = /\\n/g;
        var replaceString = '<br> \n';
        return myString.replac e(regX, replaceString);
        }

        but now I keep getting "Unterminat ed string literal" in FireFox. It's
        pointing to the space after the br tag.

        Comment

        • lkrubner@geocities.com

          #5
          Re: replace all newlines with &lt;br&gt; tags

          Okay, guessing quite randomly I got it to work with this:



          function nl2br_js(myStri ng){
          var regX = /\\n/g;
          var replaceString = '<br> \\n';
          return myString.replac e(regX, replaceString);
          }

          Comment

          Working...