开发者

Cross browser Javascript regex

开发者 https://www.devze.com 2023-01-29 07:23 出处:网络
I am using the following code to convert a dynamic string into a valid class. domain.replace(\'.\',\'_\',\'gi\')

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开发者_开发百科?


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

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

The third argument (flags) is non-standard.


You need to do it like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消