在IE7 | 8中使用特殊替换模式替换字符串的奇怪行为 [英] Odd behavior replacing string with special replacement patterns in IE7|8

查看:112
本文介绍了在IE7 | 8中使用特殊替换模式替换字符串的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用特殊替换模式时,我在IE7 | 8中发现了一个非常奇怪的问题:

I've spotted a very strange issue in IE7|8 when using special replacement patterns:

'moo$$e'.replace( /\$/g, '$$$$' );
'moo$$e'.replace( '\$', '$$$$', 'g' );   

最新Chrome:

 moo$$$$e moo$$$e

最新的Firefox:

Latest Firefox:

 moo$$$$e moo$$$$e

IE7 | 8:

 moo$$$$e moo$$$$$e

我知道 flags参数不像标准,因此在第二种情况下Firefox和Chrome之间存在差异。 我很酷#/ strong>。

I know that flags parameter is nothing like a standard, hence the difference between Firefox and Chrome in the second case. I'm cool with it.

然而,我在IE7 | 8中看到的真的很奇怪(仍然是第二种情况)。我试过玩'\ x24',转义和东西,但我找不到任何方法让它按预期工作( $$ 代表 $ )。

However, what I see in IE7|8 is really odd (still second case). I tried playing with '\x24', escaping and stuff but I cannot find any way to have this working as expected ($$ stands for $).

我知道这可以通过 split() join()轻松完成喜欢:

I know that this could be easily done with split() and join() like:

'moo$$e'.split( '$' ).join( '$$' );
> "moo$$$$e"

但我真的非常好奇IE的错误。有没有解释?

but I'm really, really curious what's wrong with IE. Is there any explanation?

请参阅实例

推荐答案

测试用例



我重新访问了测试用例,结果显示如下:

var results = [
  'YY'.replace( /Y/g, '$$' ),
  'YY'.replace( 'Y', '$$', 'g' ),
  'YY'.replace( 'Y', function( a, b ) { return '$$'; }, 'g' ),
  'YY'.replace( /Y/g, function( a, b ) { return '$$'; })  
];

console.log( results.join( '\n' ) );






结果



Chrome




Results

Chrome

$$    // '$$' -> '$', global flag used, every 'Y' -> '$'
$Y    // '$$' -> '$', global flag ignored, first 'Y' -> '$'
$$Y   // '$$' -> '$$', global flag ignored, first 'Y' -> '$$'
$$$$  // '$$' -> '$$', global flag used, every 'Y' -> '$$'



Firefox



Firefox

$$    // '$$' -> '$', global flag used, every 'Y' -> '$'
$$    // '$$' -> '$', global flag used, every 'Y' -> '$'
$$$$  // '$$' -> '$$', global flag used, every 'Y' -> '$$'
$$$$  // '$$' -> '$$', global flag used, every 'Y' -> '$$'



IE7和8



IE7 and 8

$$    // '$$' -> '$', global flag used, every 'Y' -> '$'
$$Y   // '$$' -> '$$', global flag ignored, first 'Y' -> '$$'
$$Y   // '$$' -> '$$', global flag ignored, first 'Y' -> '$$'
$$$$  // '$$' -> '$$', global flag used, every 'Y' -> '$$'






结论




Conclusions


  1. Chrome忽略'g'标志为的第三个参数String.replace ,因为以这种方式使用的标志不属于任何标准

IE假定 $$ 为字符串而不是替换控件,并在这种情况下忽略全局标志:

IE assumes $$ to be string rather that replacement control and ignores global flag in this case:

'YY'.replace('Y','$$','g');

最简单的解决方案是确保结果总是相同的是使用带有标志的 RegExp 对象( / foo / flags )作为第一个参数,字符串或函数作为第二个参数。

The simplest solution make sure that results are always the same is to use a RegExp object with flags (/foo/flags) as a first parameter and either string or function as a second parameter.

如果string是第二个参数, $$ 转换为 $ 。如果这是一个功能驱动的替代品,则没有这样的转换。

If string is a second parameter, $$ converts to $. If this is a function-driven replacement, there's no such conversion.

这篇关于在IE7 | 8中使用特殊替换模式替换字符串的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆