简体   繁体   English

跨浏览器Javascript正则表达式

[英]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. 这适用于所有主流浏览器,但不适用于Internet Explorer,我想知道为什么。 The gi flags are for global and case insensitive, but removing them means that the replace doesn't work in Firefox either. gi标志用于全局和不区分大小写,但删除它们意味着替换在Firefox中也不起作用。

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. 第三个参数(flags)是非标准的。

你需要这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM