#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    JavaScript: How to replace all occurances of chars in string


    I am new to RegEx and simply want to replace all occurrences of certain characters in a string while retaining the replaced character's case, for instance, if my string is "Anangelo" and I replace "an" with "<b>an</b>", I want the result to be :

    <b>An</b><b>an</b>gelo

    Any ideas?
  2. #2
  3. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    480
    Rep Power
    112

    Thumbs up


    Originally Posted by gangelo
    I am new to RegEx and simply want to replace all occurrences of certain characters in a string while retaining the replaced character's case, for instance, if my string is "Anangelo" and I replace "an" with "<b>an</b>", I want the result to be :

    <b>An</b><b>an</b>gelo

    Any ideas?
    You can use preg_replace() function.

    Example :

    PHP Code:
    <?php
    $str 
    "Anangelo";

    // CASE SENSITIVE //
    $output preg_replace("/An/","<b>An</b>",$str);

    // IGNORE CASE SENSITIVE //
    $output preg_replace("/An/i","<b>An</b>",$str);

    ?>
    I think you are looking somethin like this only ?
    I will try my best !!!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0
    Originally Posted by web_developer
    You can use preg_replace() function.

    Example :

    PHP Code:
    <?php
    $str 
    "Anangelo";

    // CASE SENSITIVE //
    $output preg_replace("/An/","<b>An</b>",$str);

    // IGNORE CASE SENSITIVE //
    $output preg_replace("/An/i","<b>An</b>",$str);

    ?>
    I think you are looking somethin like this only ?
    Aw man, sorry; I should have specified that I need to do this using JavaScript.

IMN logo majestic logo threadwatch logo seochat tools logo