一、下面代码的输出结果为(e)
var str1=new RegExp("e");
document.write(str1.exec("hello"));
解析:exec()是正则表达式的方法,匹配字符串中满足正则的内容,将内容作为数组返回,如果没有匹配则返回null。
对于这道题str.exec("hello")简易返回结果为 ['e'],但是由于document.write()会将内容写入文档流并在浏览器页面中显示,所以结果为 e 。
正则表达式中还有一个text方法:RegExp.prototype.test(string)。用于检测字符串是否符合某一正则,返回true或false。
如
console.log(/123/.test('123456')); // true
二、执行以下代码,输出结果为(1 3 5 4)
console.log(1);
let a = setTimeout(() => {console.log(2)}, 0);
console.log(3);
Promise.resolve(4).then(b => {
console.log(b);
clearTimeout(a);
});
console.log(5);
解析&