replace all newlines with <br> tags

Discussion in 'Javascript' started by lkrubner@geocities.com, Jan 27, 2005.

  1. Guest

    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(myString) {
    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?
     
    , Jan 27, 2005
    #1
    1. Advertising

  2. Ivo Guest

    <> wrote
    > 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(myString) {
    > 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?


    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(myString){
    return myString.replace( /\n/g, '<br />\n' );
    }

    hth
    --
    Ivo
     
    Ivo, Jan 27, 2005
    #2
    1. Advertising

  3. Guest

    Thanks much. This gives me an error "Unterminated regular expression
    literal"; I'm sorry to stay I don't know enough about Javascript or
    regular expressions to trouble shoot this.
     
    , Jan 30, 2005
    #3
  4. Guest

    I rewrite it like this:


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

    but now I keep getting "Unterminated string literal" in FireFox. It's
    pointing to the space after the br tag.
     
    , Jan 30, 2005
    #4
  5. Guest

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



    function nl2br_js(myString){
    var regX = /\\n/g;
    var replaceString = '<br> \\n';
    return myString.replace(regX, replaceString);
    }
     
    , Jan 30, 2005
    #5
    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. Rob Nicholson
    Replies:
    3
    Views:
    835
    Rob Nicholson
    May 28, 2005
  2. Viktor Rosenfeld
    Replies:
    4
    Views:
    2,024
    Viktor Rosenfeld
    Feb 2, 2004
  3. lex __
    Replies:
    3
    Views:
    610
    Neil Cerutti
    Dec 21, 2007
  4. Dotan Cohen
    Replies:
    2
    Views:
    547
    Dotan Cohen
    Oct 10, 2011
  5. Tamer Higazi
    Replies:
    3
    Views:
    149
Loading...

Share This Page