捕获括号的正则表达式拆分 - 浏览器支持: [英] Regex split by capturing parentheses - browser support :

查看:129
本文介绍了捕获括号的正则表达式拆分 - 浏览器支持:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看此示例:

>'1,2,3,4,5'.split(/,/)

结果: [1,2,3 ,4,5]

但看一下这个例子:

>'1,2,3,4,5'.split(/(,)/)

结果: [1,,,2,,,3,,,4 ,,5]

MDN


是一个包含捕获
圆括号的正则表达式,然后每次分隔符匹配时,捕获括号的结果
(包括任何未定义的结果)都是
拼接到输出数组中。 但是,并非所有浏览器都支持此
功能。

strong>

Question :

在哪里可以找到支持该功能的浏览器(和版本)列表。

Where can I find the list of browsers ( and versions) which supports that feature.

mdn

推荐答案

浏览器





Steven Levithan的博客 XRegExp 网站,确认正确的行为(包括捕获组捕获的文本)

Browser

Internet Explorer

From the blog of Steven Levithan and XRegExp website, it is confirmed that the correct behavior (inclusion of text captured by capturing groups in the result array) is not implemented up to Internet Explorer 8.

我已在 Internet Explorer 8中独立确认此结果。 a href =http://www.browserstack.com/screenshots/924eb49b33ad9761556529dab3c2dc01bd4e8cc0 =nofollow> browserstack ,并进一步确认 String.split 当提供捕获组的正则表达式正在从版本10 开始正确实施Internet Explorer。

I have independently confirmed this result on browserstack, and further confirmed that the behavior of String.split when a regex with capturing group is supplied is correctly implemented for Internet Explorer only from version 10 onwards.

截图:

  • Screenshot of Windows 7 IE 8.0
  • Screenshot of Windows 7 IE 9.0
  • Screenshot of Windows 7 IE 10.0
  • Screenshot of Windows 7 IE 11.0

测试的完整源代码网站:

Full source code of the test site:

<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("<h1>Testing String.split, given regex with capturing group</h1>");

function runTest(num, actual, expected) {
    var equals = true;

    if (actual.length === expected.length) {
        for (var i = 0; i < actual.length; i++) {
            if (actual[i] !== expected[i]) {
                equals = false;
                break;
            }
        }  
    } else {
        equals = false;
    }

    document.write("<h2>Test " + num + ":</h2>");
    document.getElementsByTagName('body')[0].appendChild(document.createTextNode("'" + actual.join("'     '") + "'"));
    document.write(equals ? "<h2>Compliant to ECMA 5.1</h2>" : "<h2>NOT compliant to ECMA 5.1</h2>");
}
</script>
<script type="text/javascript">
runTest(1, '1,2,3,4,5'.split(/(,)/), ["1", ",", "2", ",", "3", ",", "4", ",", "5"]);
</script>

<script type="text/javascript">
runTest(2, 'ABCDEF'.split(/()/), ["A", "", "B", "", "C", "", "D", "", "E", "", "F"]);
</script>

<script type="text/javascript">
runTest(3, 'text<a>text</a>'.split(/<(\/)?([^>]+)>/), ["text", void 0, "a", "text", "/", "a", ""]);
</script>
</body>
</html>

这篇关于捕获括号的正则表达式拆分 - 浏览器支持:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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