|
-
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!
-
newb of many sorts
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.
-
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.
-
newb of many sorts
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.
-
You're right! It works even though it looks like it's a comment in the editor. Thanks!
-
newb of many sorts
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|