Is this a bug in Ecmascript - / \ S / .test (null) returns true?

In both ActionScript3 and Javascript, these statements give the same result:

/\S/.test(null) => true  
/null/.test(null) => true  
/m/.test(null) => false  
/n/.test(null) => true  

It seems that in this case, a null value is converted to the string "null".

Is this a known bug in ecmascript or am I missing something?

+5
source share
2 answers

This is not a mistake, but you are right, nullcoerces to 'null'and this behavior is defined in the specification:

, , RegExp 'null', , 'n'.

var a = null+''; // 'null'
/\S/.test(a); // true
(null+'').match(/\S/) // ["n"]
+11

null - object, objects (non-string) , .

/Number/.test(Number) /String/.test(String), true.

String(null), 'null'

String(Number)

function Number() {
    [native code]
}

/function Number/.test(Number) true

+3

All Articles