-1

这个简单的正则表达式匹配在每个浏览器上返回一个字符串而不是一个对象,但最新的 firefox...

        text = "language. Filename: My Old School Yard.avi. File description: File size: 701.54 MB. View on Megavideo. Enter this, here:"
    name = text.match(/(Filename:)(.*) File /);
    alert(typeof(name));

据我所知,匹配函数应该返回一个对象(数组)。有没有人遇到过这个问题?

4

1 回答 1

1

RegExpmatch方法确实返回一个数组,但 JavaScript 中的数组只是继承自 的对象Array.prototype,例如:

var array = "foo".match(/foo/); // or [];,  or new Array();

typeof array; // "object"
array instanceof Array; // true
Object.prototype.toString.call(array); // "[object Array]"

运算符将typeof返回"object",因为它无法区分普通对象和数组。

在第二行中,我使用instanceof运算符来证明该对象实际上是一个数组,但是该运算符在跨框架环境中工作时存在已知问题。

在第三行中,我使用了该Object.prototype.toString方法,它返回一个包含[[Class]]内部属性的字符串,该属性是一个指示对象类型的值,这是一种检测对象是否为数组的更安全的方法。

于 2010-03-22T07:08:07.177 回答