正则表达式 – JavaScript:拆分在IE中不起作用?

前端之家收集整理的这篇文章主要介绍了正则表达式 – JavaScript:拆分在IE中不起作用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有一个原因导致以下代码无法在IE中运行?
使用FF和其他理智的浏览器时,它会按给定的表达式拆分字符串,在IE中它根本不起作用.
  1. var str = "abc<font id=\"something\">def</font>gh";
  2. alert(str.split(/(\<.*?\>|.)/).length);

谢谢.

你可以将下面的代码添加到你的程序中,它会起作用.
  1. var split;
  2. // Avoid running twice; that would break the `nativeSplit` reference
  3. split = split || function (undef) {
  4.  
  5. var nativeSplit = String.prototype.split,compliantExecNpcg = /()??/.exec("")[1] === undef,// NPCG: nonparticipating capturing group
  6. self;
  7.  
  8. self = function (str,separator,limit) {
  9. // If `separator` is not a regex,use `nativeSplit`
  10. if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
  11. return nativeSplit.call(str,limit);
  12. }
  13. var output = [],flags = (separator.ignoreCase ? "i" : "") +
  14. (separator.multiline ? "m" : "") +
  15. (separator.extended ? "x" : "") + // Proposed for ES6
  16. (separator.sticky ? "y" : ""),// Firefox 3+
  17. lastLastIndex = 0,// Make `global` and avoid `lastIndex` issues by working with a copy
  18. separator = new RegExp(separator.source,flags + "g"),separator2,match,lastIndex,lastLength;
  19. str += ""; // Type-convert
  20. if (!compliantExecNpcg) {
  21. // Doesn't need flags gy,but they don't hurt
  22. separator2 = new RegExp("^" + separator.source + "$(?!\\s)",flags);
  23. }
  24. /* Values for `limit`,per the spec:
  25. * If undefined: 4294967295 // Math.pow(2,32) - 1
  26. * If 0,Infinity,or NaN: 0
  27. * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
  28. * If negative number: 4294967296 - Math.floor(Math.abs(limit))
  29. * If other: Type-convert,then use the above rules
  30. */
  31. limit = limit === undef ?
  32. -1 >>> 0 : // Math.pow(2,32) - 1
  33. limit >>> 0; // ToUint32(limit)
  34. while (match = separator.exec(str)) {
  35. // `separator.lastIndex` is not reliable cross-browser
  36. lastIndex = match.index + match[0].length;
  37. if (lastIndex > lastLastIndex) {
  38. output.push(str.slice(lastLastIndex,match.index));
  39. // Fix browsers whose `exec` methods don't consistently return `undefined` for
  40. // nonparticipating capturing groups
  41. if (!compliantExecNpcg && match.length > 1) {
  42. match[0].replace(separator2,function () {
  43. for (var i = 1; i < arguments.length - 2; i++) {
  44. if (arguments[i] === undef) {
  45. match[i] = undef;
  46. }
  47. }
  48. });
  49. }
  50. if (match.length > 1 && match.index < str.length) {
  51. Array.prototype.push.apply(output,match.slice(1));
  52. }
  53. lastLength = match[0].length;
  54. lastLastIndex = lastIndex;
  55. if (output.length >= limit) {
  56. break;
  57. }
  58. }
  59. if (separator.lastIndex === match.index) {
  60. separator.lastIndex++; // Avoid an infinite loop
  61. }
  62. }
  63. if (lastLastIndex === str.length) {
  64. if (lastLength || !separator.test("")) {
  65. output.push("");
  66. }
  67. } else {
  68. output.push(str.slice(lastLastIndex));
  69. }
  70. return output.length > limit ? output.slice(0,limit) : output;
  71. };
  72.  
  73. // For convenience
  74. String.prototype.split = function (separator,limit) {
  75. return self(this,limit);
  76. };
  77.  
  78. return self;
  79. }();

猜你在找的正则表达式相关文章