;

PDA

Click to See Complete Forum and Search --> : Help with regular expressions


Burvs
08-26-2009, 07:46 AM
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:

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!

Ralgoth
08-26-2009, 07:55 AM
var str:String = "things/stuff/cheese/yum";
var myPattern:RegExp = /\//g;
trace(str.replace(myPattern, "."));
// returns: "things.stuff.cheese.yum"

Burvs
08-26-2009, 08:02 AM
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.

Ralgoth
08-26-2009, 08:05 AM
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?

Burvs
08-26-2009, 08:11 AM
You're right! It works even though it looks like it's a comment in the editor. Thanks!

Ralgoth
08-26-2009, 08:20 AM
my pleasure :)