Results 1 to 6 of 6

Thread: Find and Replace in JavaScript String

  1. #1
    Join Date
    Oct 2008
    Posts
    127

    Find and Replace in JavaScript String

    I can update or replace part of a string by another string in c program it is also possible in C++ program but the same logic doesn't work JavaScript. Can any one tell me the function which is used in java script to search and replace string.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Find and Replace in JavaScript String

    String functions are available in programming languages like JavaScript.JavaScript's String Object has a handy function that lets you replace words that occur within the string. These string functions allow manipulation and comparison of strings, including replacement of characters with other characters.

  3. #3
    Join Date
    Jan 2009
    Posts
    199

    Re: Find and Replace in JavaScript String

    Code:
    <script type="text/javascript">
    var visitorName = "chk";
    var myOldString = "Hello chk";
    var myNewString = myOldString.replace("username", visitorName);
    
    document.write("Old string =  " + myOldString); 
    document.write("<br />New string = " + myNewString);
    
    </script>

  4. #4
    Join Date
    Dec 2008
    Posts
    177

    Re: Find and Replace in JavaScript String

    Code:
    function replace( str, from, to ) {
        var ix = str.indexOf( from );
    
    
        while ( ix > -1 ) {
            str = str.replace( from, to ); 
            ix = str.indexOf( from );
        }
    
        return str;
    }
    
    var string = "{7} 123 Main St {8} Vancouver CA {4}";
    
    string = replaceAll( string, "{7}", "#" );
    string = replaceAll( string, "{8}", "," );
    string = replaceAll( string, "{4}", " " );
    
    alert( string );

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Find and Replace in JavaScript String

    The JavaScript function for String Replace replaces the first occurrence in the string. The function is similar to the php function str_replace and takes two simple parameters. Javascript's built-in String replace method String::replace() is a pretty darn powerful method. The first parameter is the pattern to find and the second parameter is the string to replace the pattern with when found.

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: Find and Replace in JavaScript String

    Code:
    <script type="text/javascript">
    
    var str="javascript string ";
    document.write(str.replace("javascript", "string"));
    
    </script>

Similar Threads

  1. Coversion of Cast string as int in Javascript
    By LaMarcus in forum Software Development
    Replies: 5
    Last Post: 14-10-2010, 03:59 AM
  2. Replace backslash in a string
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 05:43 AM
  3. Simple string replace in php
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 01:37 PM
  4. Trim a String in JavaScript
    By Kingfisher in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 10:32 AM
  5. Get String Length with Javascript
    By klite in forum Software Development
    Replies: 3
    Last Post: 31-10-2009, 11:39 AM

Tags for this Thread

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
  •  
Page generated in 1,714,160,550.53152 seconds with 17 queries