如何检索正则表达式实例的相应对象文字?

时间:2017-05-21 19:40:09

标签: javascript regex

要在控制台中检索全局String对象实例的文字对象形式,我们只需执行以下操作:

SELECT
constraint_name
FROM
 information_schema.REFERENTIAL_CONSTRAINTS
WHERE
constraint_schema = 'Blog' AND table_name = 'posts';


ALTER TABLE posts DROP Foreign Key keyname;

但是当一个人创建一个全局RegExp对象的正则表达式实例并尝试获取对象文字形式时,它就不会工作,而控制台只会输出正则表达式模式和标志。

var myString = new String("Hello Stackoverflow!");
console.log(myString); 

/* console outputs: String {0: "H", 1: "e", 2: "l",..., 18: "w", 
19: "!", length: 20, [[PrimitiveValue]]: "Hello Stackoverflow!"} */

如何检索具有所有属性的正则表达式对象实例并在控制台中显示它?

3 个答案:

答案 0 :(得分:2)

实际上RexExp的所有属性都不可枚举,因此无法以非常简单的方式显示。 此外,覆盖对象的toString()方法可以更改要打印的内容。例如:



var myRegexp = new RegExp("\\d+","g");
myRegexp.toString = function() {
  return 'I am a regex and I dont want to show my properties!';
};
console.log(myRegexp); 




这么说,我在MDN帖子后面创建了一个jsfiddle(链接将会跟随),它将打印你想要的所有属性。我刚刚在jsfiddle中实现了一个示例,但是你需要对它进行一些操作,以便根据需要获得打印并使用你想要的正确属性



var SimplePropertyRetriever = {
    getOwnEnumerables: function(obj) {
        return this._getPropertyNames(obj, true, false, this._enumerable); 
         // Or could use for..in filtered with hasOwnProperty or just this: return Object.keys(obj);
    },
    getOwnNonenumerables: function(obj) {
        return this._getPropertyNames(obj, true, false, this._notEnumerable);
    },
    getOwnEnumerablesAndNonenumerables: function(obj) {
        return this._getPropertyNames(obj, true, false, this._enumerableAndNotEnumerable); 
        // Or just use: return Object.getOwnPropertyNames(obj);
    },
    getPrototypeEnumerables: function(obj) {
        return this._getPropertyNames(obj, false, true, this._enumerable);
    },
    getPrototypeNonenumerables: function(obj) {
        return this._getPropertyNames(obj, false, true, this._notEnumerable);
    },
    getPrototypeEnumerablesAndNonenumerables: function(obj) {
        return this._getPropertyNames(obj, false, true, this._enumerableAndNotEnumerable);
    },
    getOwnAndPrototypeEnumerables: function(obj) {
        return this._getPropertyNames(obj, true, true, this._enumerable); 
        // Or could use unfiltered for..in
    },
    getOwnAndPrototypeNonenumerables: function(obj) {
        return this._getPropertyNames(obj, true, true, this._notEnumerable);
    },
    getOwnAndPrototypeEnumerablesAndNonenumerables: function(obj) {
        return this._getPropertyNames(obj, true, true, this._enumerableAndNotEnumerable);
    },
    // Private static property checker callbacks
    _enumerable: function(obj, prop) {
        return obj.propertyIsEnumerable(prop);
    },
    _notEnumerable: function(obj, prop) {
        return !obj.propertyIsEnumerable(prop);
    },
    _enumerableAndNotEnumerable: function(obj, prop) {
        return true;
    },
    // Inspired by http://stackoverflow.com/a/8024294/271577
    _getPropertyNames: function getAllPropertyNames(obj, iterateSelfBool, iteratePrototypeBool, includePropCb) {
        var props = [];

        do {
            if (iterateSelfBool) {
                Object.getOwnPropertyNames(obj).forEach(function(prop) {
                    if (props.indexOf(prop) === -1 && includePropCb(obj, prop)) {
                        props.push(prop);
                    }
                });
            }
            if (!iteratePrototypeBool) {
                break;
            }
            iterateSelfBool = true;
        } while (obj = Object.getPrototypeOf(obj));

        return props;
    }
};
var myRegexp = new RegExp("\\d+","g");
SimplePropertyRetriever.getPrototypeNonenumerables(myRegexp).forEach(function(el) {
	console.log(el + ": " + myRegexp[el]);
});




这里有链接:

https://developer.mozilla.org/it/docs/Web/JavaScript/Enumerability_and_ownership_of_properties

这是一个jsfiddle:

https://jsfiddle.net/t3bp4tnq/1/

我希望这会有所帮助

答案 1 :(得分:1)

console.dir打印对象的树形表示。

console.dir(myRegexp);

答案 2 :(得分:0)

source的{​​{1}}和global属性都不可枚举。您可以通过特定的属性

来获取属性的值

myRegexp