Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder tylerjca's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    161
    Thanks
    1
    Thanked 0 Times in 0 Posts

    using replace() to change string

    I know how to do the following with PHP:
    PHP Code:
    $text "<msg>this is a message</msg>";
     
    $text preg_replace("(<msg>(.*?)</msg>)i","<b>$1</b>",$text); 
    but how can i do the same with javascript?

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    Let's assume that you have a string called user_name and contains the value "John Smith"....and you wish to replace Smith with Bond. Here is how.

    <script type="text/javascript">

    var user_name = "John Smith";

    user_name = user_name.replace( "Smith", "Bond" );

    </script>

    Please note that you can use regular expressions to search for values...

    good luck
    ess

  • #3
    Regular Coder tylerjca's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    161
    Thanks
    1
    Thanked 0 Times in 0 Posts
    what you just showed me i know how to do, but i just want the regular expression and replace equivalent for javascript..

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about this.

    Code:
    <html>
      <head>
        <script type="text/javascript">
          var text = "<msg>this is a message</msg>";
          var reg = /<msg>(.*?)<\/msg>/i;
    
          text = text.replace(reg, "<b>$1</b>");
          alert(text);
        </script>
      </head>
      <body>
      </body>
    </html>
    david_kw

    P.S. As a side note, does the ? in the reg expression do anything?

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
    var
        re=/<msg>(.*?)<\/msg>/gi,
    // Warning on using non-greedy matches.
    // They have bad backwards compatibility
    // and still don't have quite correct support in JScript
        sOrig='<msg>this is a message<\/msg> and <msg>this is another message<\/msg>',
        sRes=sOrig.replace(re,'<b>$1<\/b>');
    
    alert(['sRes: '+sRes,'sOrig: '+sOrig].join('\n'));
    
    // Beware! Potential problem:
    sOrig='<msg>this is <msg>a message containing a nested<\/msg> message<\/msg> and <msg>this is another message<\/msg>';
    sRes=sOrig.replace(re,'<b>$1<\/b>');
    
    alert(['sRes: '+sRes,'sOrig: '+sOrig].join('\n'));
    
    // One possible solution:
    sRes=sOrig;
    while(re.test(sRes))
        sRes=sRes.replace(re,'<b>$1<\/b>');
    
    alert(['sRes: '+sRes,'sOrig: '+sOrig].join('\n'));
    Last edited by liorean; 11-12-2006 at 01:21 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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