正则表达式匹配()适用于FF / Chrome但不适用于IE 8 [英] regex match() works in FF/Chrome but not IE 8

查看:304
本文介绍了正则表达式匹配()适用于FF / Chrome但不适用于IE 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下jQuery调用,它返回FF / Chrome中的匹配,但在IE 8中返回null。

I have the following jQuery call, which returns a match in FF/Chrome, but returns null in IE 8.

如果你想亲自尝试一下,这就是小提琴。

Here's the fiddle if you'd like to try it for yourself.

这里是不可解决的,不可行的,任性的代码:

And here's the insoluble, unpliable, wayward code:

var m = $('#somediv').text().match(/\d+-(\d+)\sof\s(\d+)/);

编辑:感谢Rob W.我已经缩小了一点;以下是有效的,所以它特别是失败的of或\sof\s。把小提琴分开并为自己尝试一些:(

Thanks to Rob W. I've narrowed this a bit; the following works, so it's specifically the " of " or "\sof\s" that fails. Fork the fiddle and try a few for yourself :(

var m = $('#somediv').text().match(/\d+\D(\d+)\D+(\d+)/);


推荐答案

基于Rob W的答案,该问题解决了替换的问题,默认情况下不会全局替换,另外一件事就是你要搜索的实际文本.Rob W还指出,既然你用jQuery的 text()获得了文本,那么& nbsp; 实体已被解码为实际的,非实际的不间断空格字符。

Building on Rob W's answer, which picks up on the problem that replace doesn't replace globally by default, the other thing that's off here is the actual text you're searching for. Rob W also points out that since you're getting the text with jQuery's text(), the   entities have been decoded to actual, factual non-breaking space characters.

& nbsp; 是一个HTML实体,将其指定为的参数实际上不会将其解释为不间断的空格,它只是在主题字符串中查找实际文本& nbsp;

  is a HTML entity, specifying it as an argument to replace isn't going to actually interpret it as a non-breaking space, it's just going to look for the actual text   in the subject string.

指定Unicode搜索中不间断空格(00A0)的代码点正则表达式在IE 8和IE 7兼容模式下为我工作:

Specifying the Unicode codepoint for the non-breaking space (00A0) in the search regex worked for me in IE 8 and IE 7 compatibility mode:

var m = t.replace(/\u00a0/g, ' ').match(/\d+-(\d+)\sof\s(\d+)/);

在IE 9中,东西正常工作,因为它和其他浏览器一样,在<$中包含不间断的空格C $ C> \s IE的旧版本没有,这是唯一的原因需要更换。

Things work correctly in IE 9 because it, like other browsers, includes non-breaking spaces in \s. Older versions of IE don't, and that's the only reason the replacement would be necessary.

这篇关于正则表达式匹配()适用于FF / Chrome但不适用于IE 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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