Results 1 to 2 of 2

Thread: I need help with some javascript:

  1. #1
    Join Date
    Oct 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help with some javascript:

    I am trying to replace [[x y z]]
    with
    <a href="http://en.wikipedia.org/wiki/x_y_z">x y z</a>".
    , similarly i am trying to replace [[x#y]]
    with
    <a href="http://en.wikipedia.org/wiki/x#y">x#y</a>
    -----
    here is what I have so far:
    <html>
    <head>
    <title>Project.Link</title>
    </head>
    <body>
    <script type= "text/javascript">
    var input = window.prompt("Enter Text Here");
    for(i=0;i<=input.length;i++)
    {
    var openbrackets = input.indexOf("[[");
    var closebrackets = input.indexOf("]]");
    var linkstart = openbrackets+2;
    var linkend = closebrackets--;
    var link = input.substring(linkstart, linkend);
    }
    for (i=linkstart;i<=linkend;i++)
    {
    if (link.charAt(i) == " ")
    {
    var replace_ = link.replace(" ", "_");
    }
    }
    for (i=0;i<=input.length;i++)
    {
    var replaceo = input.replace("[[", '<a href="http://en.wikipedia.org/wiki/'+replace_+'">');
    var replacec = input.replace("]]", '</a>');
    }
    var sentence = replaceo.concat(link);
    var tsentence= document.write(sentence.concat(replacec));
    </script>
    </body>
    </html>

    ------

    Can anyone help me? Where is my code wrong?!?!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There's an easier way:
    Code:
    function wikify(str) {
      return str.replace(/\[\[([^\]]+)\]\]/g, function() {
        return '<a href="' + encodeURIComponent(arguments[1].replace(" ", "_")) + '">' + arguments[1] + "</a>";
    });
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •