Problems with "split/replace"

Discussion in 'JavaScript' started by KaiserClaw, Aug 12, 2009.

  1. #1
    Hi!

    I have a webpage (HTML) and an textbox where I write data.

    The strings looks like this "0000020300024000500000000200908111024211 Hi how are you"

    I just want the first 8 digits without zeros in the beginning.

    example:

    00030500001 => 30560
    12345678000024 => 12345678

    I want to strip the "00000" and the digits after 203 so the script alerts:

    203
    Hi
    how
    are
    you

    I want the script to look like this in the end -> "alert (number +<br /> +text1 +<br /> +text2 +<br /> +text3 +<br /> +text4);

    The problem:

    * I can just replace the zeros in front of 203, but not the other digits.

    * I can't put the text in variabel "text1-4"

    * It can't handle multilines, just the first line.

    Output: "203 Hi how are you" - it only takes the first line, "Hi how are you" is still there and ignores the second line....

    I want it to look like this:

    "203"
    "204"

    The script looks like this:

    function split()
    {
    var output = document.getElementById("textfield").value;

    var result = output.replace(/^[0]+/g,"");

    alert("result");


    }
     
    KaiserClaw, Aug 12, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,

    if I understood correctly you could do it like:

    var str = "0000020300024000500000000200908111024211 Hi how are you";
    var num = str.substr(0,8).replace(/^[0]+/g, '');
    var txt = str.replace(/[0-9]+/g, '').split(' ');
    alert(num + txt.join('<br />'));

    Is it what you needed?
     
    lp1051, Aug 12, 2009 IP