使用正则表达式很愚蠢.如果你真的只想匹配字符串'true'而不关心区分大小写,那么只需比较该字符串:
function parseBoolean(s)
{
return s === 'true';
}
Run Code Online (Sandbox Code Playgroud)
我想如果我真的想和你一起玩"填补空白",我会回答:
+"10" is to Number("10") as !!"true" is to Boolean("true")
Run Code Online (Sandbox Code Playgroud)
因为Boolean(x)只有false当x一个假值,即何时返回x ? {null, undefined, false, 0, ''}.