can I set a regexp for an object's method/property selector?

后端 未结 3 457
小鲜肉
小鲜肉 2021-01-07 03:35
var regxp = /[\\S]/; //any char, not sure if it\'s /.*/ or something else
var obj = {
 atr1: \"bla\"
}
var blahs = obj[regxp]; //returns atr1

I\'m

3条回答
  •  孤街浪徒
    2021-01-07 03:48

    your regex will match a single non-space character.

    for...in is a loop. it's slower than what exactly? have you benchmarked?

    if you want to look up properties using a regex, you'll have to do it in a loop.

    for(var k in obj) {
        if(regexp.match(k)) {
          // do whatever
        }
    }
    

提交回复
热议问题