SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    529
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression to handle multiple replaces

    this is what i have right now...

    Code JavaScript:
    var regex = new RegExp('/', 'g');
    var regex2 = new RegExp('_', 'g');
     
    myArray[2].replace(regex, ' > ').replace(regex2, ' ');

    basically it's replacing '/' with ' > ' and '_' with ' '. but is there a way i can combine these two somehow so i don't have to declare two different expressions and run the replace method twice?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, you could if you used a function for the second parameter, but to be honest it's easier to do the following (you don't even need regular expressions:
    Code:
    myArray[2].replace('/', ' > ').replace('_', ' ');


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
  •