SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Replace Javascript function

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    html,body{height:100&#37;}
    </style>
    <script type="text/javascript">
    function strRep(){
    var text="class II*Class V | Class II*Class V|5";
    alert(text);
    var text=text.replace("*",";");
    alert(text);

    }
    </script>
    </head>
    <body onclick="strRep();">

    </body>
    </html>
    i am trying to replace all occurance of * from string but above code is replacing only 1st occurance of * , can any one help me why

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use a regexp with the global modifier.
    Code:
    text=text.replace(/\*/g,";");
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternatively you can repeatedly run the string method-
    while(text.indexOf('*')!=-1) text=text.replace("*",";");

    But kravvitz's example is a good way to start using regular expressions, if you haven't yet used them.

  4. #4
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kool ...
    thanks


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
  •