Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 840
David Yell Avatar asked Dec 06 '10 17:12

David Yell


2 Answers

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

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

The third argument (flags) is non-standard.

like image 141
Matti Virkkunen Avatar answered Sep 23 '22 01:09

Matti Virkkunen


You need to do it like this:

domain.replace(/\./g, '_');
like image 27
jwueller Avatar answered Sep 22 '22 01:09

jwueller