0

let ob = {};
ob[/\ing?$/] = `I match all strings end with "ing"`; //key is instance of 'RegEXp'
ob["/\ing?$/"] = `I am sure it is not same as the above`; //key is instance of 'String'

console.log(
`- Key RegExp :
   ==> ${ob[/\ing?$/]}
`);

console.log(
`- Key String :
   ==> ${ob["/\ing?$/"]}
`);

The above string demonstrates that literal object property can be an instance of RegExp class and it can be also String of course and they are totally different .

My question is how to check if the type property using Object.keys or alternative . Known that using Object.keys, this method casts all keys (properties) to string ?

Object.keys(ob);
 //--> Expected :[/\ing?$/, "/\ing?$/"]
 //--> Actual : ["/\ing?$/", "/ing?$/"]
5
  • You cannot use Objects, you can use array to store regex. Commented Nov 8, 2016 at 3:51
  • Object key will be converted to string. Commented Nov 8, 2016 at 3:51
  • What are you trying to achieve? Commented Nov 8, 2016 at 3:53
  • key is instance of 'RegEXp' - are you sure about that? typeof disagrees with your assertion Commented Nov 8, 2016 at 3:54
  • Does the RegExp match the example string? Commented Nov 8, 2016 at 4:51

5 Answers 5

2

Property names are always strings. If you try to use something else as a property name, it will be converted to a string using toString(). So when you do:

ob[/\ing?$/] = `I match all strings end with "ing"`; //key is instance of 'RegEXp';

it's treated as

ob[/\ing?$/.toString()] = `I match all strings end with "ing"`; //key is instance of 'RegEXp';

So the key is "/\ing?$/".

When you do:

ob["/\ing?$/"] = `I am sure it is not same as the above`; //key is instance of 'String';

the key is the string "/ing?$/". The backslash is gone because in a string literal, backslash is an escape character. \i just escapes the i character, but since i has no special meaning, it's just i.

If you wanted to get the same key as from the RegExp, it should be:

ob["/\\ing?$/"] = `I am sure it is not same as the above`; //key is instance of 'String';

The double backslash escapes the backslash, so it gets put into the string literally.

let ob = {};
ob[/\ing?$/] = `I match all strings end with "ing"`; //key is instance of 'RegEXp';
ob["/\\ing?$/"] = `I am sure it is not same as the above`; //key is instance of 'String';
console.log(ob);

Sign up to request clarification or add additional context in comments.

Comments

0

The above string demonstrates that literal object property can be an instance of RegExp class

That is wrong. All object keys are string. Anything that is not string will be converted to string. The proof can be seen in code sample below. Although o1 and o2 are different, they both have same string representation and can be used to access the property of o.

var o1 = {};
o1.toString = function(){return "I am unique";};

var o2 = {};
o2.toString = function(){return "I am unique";};


var o = {};
o[o1] = "foo";



console.log(o[o2]);

Comments

0

The properties are being converted to strings when they're set:

for (var key in ob) {
    console.log('key', key, 'type', typeof key);
}

> key /\ing?$/ type string
> key /ing?$/ type string

Comments

0

The RegExp at javascript at Question does not match the example string.

let re = /\ing?$/;
console.log(re.test(`I match all strings end with "ing"`));

You can use RegExp /(.|)ing?(\1|\.)$/ to match string ending with ing or ing., or "ing" define property and value of object as RegExp

const re = /(.|)ing?(\1|\.)$/;

let ob = {[re]:re};

console.log(ob[re].test(`I match all strings end with "ing"`));

console.log(ob[re].test(`I match all strings end with ing.`));

console.log(ob[re].test(`I match all strings end with ing`));

console.log(Object.getOwnPropertyDescriptors(ob));

for (let {[re]:regexp} of [ob]) {
  console.log(regexp instanceof RegExp);
}

Comments

0

Since I am the owner of question, The best solution that i noticed is to change the data structure from literal object to array :

From :

ob = {
   /\ing?$/ : `I match all strings end with "ing"`,
  "/\ing?$/" : `I am sure it is not same as the above`
};

to :

ob = [

   {key: /\ing?$/ , value: `I match all strings end with "ing"` },
   {key:"/\ing?$/" , value: `I am sure it is not same as the above` },  
]

By that, we conserve the type of key if it is RegExp instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.