regex match() works in FF/Chrome but not IE 8

前端 未结 3 1682
自闭症患者
自闭症患者 2021-01-21 11:43

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 her

3条回答
  •  孤城傲影
    2021-01-21 12:30

    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.

      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.

    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+)/);
    

    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.

提交回复
热议问题