RegEx无法在IE8中运行 [英] RegEx not working in IE8

查看:78
本文介绍了RegEx无法在IE8中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,在这里和谷歌上搜索高低之后我不得不咬紧牙关并在IE8中询问有关RegEx的新问题。

OK, after searching high and low both here and on Google I have to bite the bullet and ask a new question here regarding RegEx in IE8.

我有以下代码:

var output = input.replace(/ ^ \S +(\\\ |& nbsp;)+ /,' ');

其目的是从金额中删除货币名称,因此kr 12 345,00变为12 345,00并且NOK 12 345,00变为12 345,00等等。

Its purpose is to remove currency names from amounts, so "kr 12 345,00" becomes "12 345,00" and "NOK 12 345,00" becomes "12 345,00" and so on.

除了Internet Explorer 8或更低版本之外,其他地方都可以正常工作>它保持输入不变。

This works fine everywhere except in Internet Explorer versions 8 or below, where it leaves the input unchanged.

最初我使用的是新的RegExp()方式这样做,但将其更改为正则表达式文字以查看IE如何处理这两个变体之间存在差异 - 幸运的是,没有:)

Initially I was using the new RegExp() way of doing it, but changed it to the regular expression literal to see if there was a difference between how IE handled those two variations - which, thankfully, there wasn't :)

我还尝试过只需 \s 而只使用& nbsp; 但是没有做出改变。

I have also tried with just the \s and with just the   but that didn't make a difference.

真的 奇怪的是当用kr 12 345,00运行上面的正则表达式时在IE 8中输入 W3学校的自己动手页精细! (当第一个空格用& nbsp; 代替时)

The really weird thing is that when running the above regex with "kr 12 345,00" as the input on W3 School's "Try It Yourself" page in IE 8 it works fine! (also when the first space is substituted with  )

重申:我所在的地方我看到这个问题是在我的Javascript代码中使用正则表达式,它存在于AngularJS过滤器函数中(我正在定义一个过滤器),并且过滤器在IE8中返回原始输入(也可以在console.log中看到) )在IE8中)。有人在IE9中为我测试了它,并报告它在那里工作得很好 - 我也在Chrome中测试过。我遇到过这个问题的唯一浏览器是IE8和IE7。

So to reiterate: The place I'm seeing this issue is when using the regex in my Javascript code, which resides in an AngularJS filter function (I'm defining a filter), and the filter is returning the original input in IE8 (which is also seen with console.log() in IE8). Someone else tested it in IE9 for me, and reported that it worked fine there - and I have tested in Chrome as well. The only browsers in which I've encountered this issue are IE8 and IE7.

这里到底发生了什么?

推荐答案

尝试

var output = input.replace(/^\S+([\s\xA0]| )+/, '');

根据这里,在IE8中...... 。\(其中应包括所有空格)不包括不间断的空格。

According to here, "in IE8... \s (which should include all white space) doesn’t include non-breaking space".

或者更好,只需使用

var output = input.replace( /^\D+/, '' );

其中 \D 是非任何非数字字符。

where \D is any non-digit character.

这篇关于RegEx无法在IE8中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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