Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/NodeJS: named group RegExp match: what [Object: null prototype] does it mean?

see the NodeJs, here below:

$ node
Welcome to Node.js v12.3.1.

> regexp = /cerca (?<word>.+) su (?<dictionary>wikipedia|treccani|garzanti|google)/i

> string = 'cerca chatbots su wikipedia'

> matchData = string.match(regexp)
[
  'cerca chatbots su wikipedia',
  'chatbots',
  'wikipedia',
  index: 0,
   input: 'cerca chatbots su wikipedia',
   groups: [Object: null prototype] {
     word: 'chatbots',
     dictionary: 'wikipedia'
  }
]

> matchData.groups.word
'chatbots'

> matchData.groups.dictionary
'wikipedia'

The regexp match appear OK to me, and named groups are captured perfectly, but what does it mean the [Object: null prototype] statement in console.log node REPL?

thanks

like image 832
Giorgio Robino Avatar asked Dec 08 '25 09:12

Giorgio Robino


1 Answers

The [Object: null prototype] means that the __proto__ atribute of the object is equals to undefined. The _proto_ atribute is used on javascript heritance, so it is used on log to auxiliate the identification of classes.

It occurs due to the fact that groups have as keys only the found ones (missing the __proto__). And in that way, you can iterate over keys with for(let key in matchData.groups){...} with 100% of accuracy that key wont assume the "__proto__" value. This design may be unnescessary, but it works.

You can create a common object using Object(matchData.groups) or {...matchData.groups}

like image 82
yolisses Avatar answered Dec 09 '25 23:12

yolisses