David Yell
David Yell

Reputation: 11855

Cross browser Javascript regex

I am using the following code to convert a dynamic string into a valid class.

domain.replace('.','_','gi')

This works fine in all major browsers, but not in Internet Explorer and I'm wondering why. The gi flags are for global and case insensitive, but removing them means that the replace doesn't work in Firefox either.

Any ideas on how I change this to make it more friendly with more browers?

Upvotes: 6

Views: 1141

Answers (2)

jwueller
jwueller

Reputation: 30996

You need to do it like this:

domain.replace(/\./g, '_');

Upvotes: 7

Matti Virkkunen
Matti Virkkunen

Reputation: 65116

You'll need to use an actual regexp instead of a string:

domain.replace(/\./g, "_")

The third argument (flags) is non-standard.

Upvotes: 10

Related Questions