A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Help with regular expressions

  1. #1
    Junior Member
    Join Date
    Aug 2008
    Posts
    12

    Help with regular expressions

    Hi all,

    I'm trying to use regular expressions with the String.replace() method to manipulate a string to replace all instances of a forward slash "/" with a period "."

    So for instance, if I start with this String:
    "things/stuff/cheese/yum"

    I want to convert it to:
    "things.stuff.cheese.yum"

    Easy enough if I want to replace a letter... let's say I wanted to replace all instances of the letter "e", I would do this:

    Code:
    var str:String = "things/stuff/cheese/yum";
    var myPattern:RegExp = /e/g;  
    trace(str.replace(myPattern, "."));
    // returns: "things/stuff/ch..s./yum"
    ... but how do I replace all instances of "/"??

    Thanks!

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    Code:
    var str:String = "things/stuff/cheese/yum";
    var myPattern:RegExp = /\//g; 
    trace(str.replace(myPattern, "."));
    // returns: "things.stuff.cheese.yum"
    Last edited by Ralgoth; 08-26-2009 at 08:00 AM.
    Search first, asked questions later.

  3. #3
    Junior Member
    Join Date
    Aug 2008
    Posts
    12
    That doesn't work for me, Ralgoth. If you keep the quotes around the regular expression, it views it as a String, not a RegExp, and if you remove them, it treats the double slash as a comment.

  4. #4
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    I had quotes around it because the forum kept autoformatting the expression... I realized i used the PHP tag instead of CODE (I edited it above, should appear correctly).

    Anyway, the backslash should overwrite the commenting. Some of it may appear green in the code viewer, but it works on my end. Did you run it?
    Search first, asked questions later.

  5. #5
    Junior Member
    Join Date
    Aug 2008
    Posts
    12
    You're right! It works even though it looks like it's a comment in the editor. Thanks!

  6. #6
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    my pleasure
    Search first, asked questions later.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center