SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Örebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Catching values from a regular expression

    Hi,

    Is it possible to catch the found values when replacing parts of a string using a regular expression?
    For example, this is how you do when using preg_replace() in PHP:

    PHP Code:
    $string '/path/to/image.gif';
    $string preg_replace('/^(?:.*?)\/([^.]+\.gif)$/i''\1'$string);
    // $string now contain "image.gif" 
    Is it possible to do the same thing using Javascript?

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  2. #2
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi erik,

    see if this example helps you

    var str = " Vikrant ";
    var abcd = str.replace(/^\s+/g, '').replace(/\s+$/g, '');
    alert("'" + abcd + "'");


    this will give you an alert saying 'Vikrant' i.e. removes the blank spaces. and it uses regular expression.

    Let me know if you need more help
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  3. #3
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Örebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for your reply. However, I think that you have misunderstood what my question is... I do not want to remove any whitespaces, I want to obtain the values found by my regular expression, and use them in the replacement argument. Let me give you an example. We assume that I want to make all instances of foo, bar and foobar bold. Then I would go ahead and do something like this in PHP:

    PHP Code:
    $string 'my foo is bar and foobar';
    $string preg_replace('/(foo|bar|foobar)/''<b>\1</b>'$string); 
    Notice how I obtain the found value using \1 in the replacement argument for preg_replace(). That is what I want to accomplish using Javascript (if possible, that is).

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  4. #4
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Strömstad
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Test with $1.

  5. #5
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Örebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks, that worked like a charm. Here is an example for those who want it:

    HTML Code:
    <script type="text/javascript">
     var string = 'hello. my foo is the bar, with the foobar!';
     document.writeln(string.replace(/(foo|bar|foobar)/g, '<b>$1</b>'));
    </script>
    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.


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
  •