How do I get the corresponding object literal of a regex instance?

To get the literal form of an instance of a global String object in the console, we simply do:

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!"} */

      

But when you instantiate the regex of the global RegExp object and try to get the literal form of the object, it won't work and the console just throws the regex pattern and flags.

var myRegexp = new RegExp("\\d+","g");
console.log(myRegexp); 

/* console outputs: /\d+/g while I would expect RegExp{..., global:true,...} 
basically the look of an object with curly braces and properties*/

      

How can I get an instance of a regex object with all its properties and display it in the console?

+3


source to share


3 answers


console.dir prints a tree view of an object.



console.dir(myRegexp);

      

+1


source


In fact, all RexExp properties are not enumerable, so they cannot be shown very easily. Moreover, by overriding an toString()

object's method , you can change what will be printed. For example:

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

Run codeHide result


That being said, I created a jsfiddle following the MDN post (link will follow) that will output all required properties. I just implemented a sample in jsfiddle and here, but you need to play around a little with it to get a printout as you see fit and with the properties you want.

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]);
});
      

Run codeHide result




Here's the link:

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

Here's the jsfiddle:

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

I hope this helps

+2


source


Both properties are source

and are global

myRegexp

not enumerable. You can get property values ​​by specific property reference

var myRegexp = new RegExp("\\d+","g");

var myRegexpProps = {};

var props = ["source", "flags", "global", "multiline", "sticky", "unicode"];

for (let prop of props) myRegexpProps[prop] = myRegexp[prop];

console.log(myRegexp.source, myRegexp.global, myRegexp.flags
           , myRegexp.ignoreCase, myRegexp.multiline);
           
console.log(myRegexpProps);
      

Run codeHide result


0


source







All Articles