js中为什么字符串中加了\n匹配就一直失败

这样没结果

var s = 'hi\nabc454def';
console.log(/h.+(\d+)def/g.exec(s));

这样是有结果的

var s = 'hiabc454def';
console.log(/h.+(\d+)def/g.exec(s));
阅读 5k
5 个回答
var s = 'hi\nabc454def';
console.log(/h[\s\S]+(\d+)def/g.exec(s));//["hi↵abc454def", "4", index: 0, input: "hi↵abc454def"]  
console.log(/h[\w\W]+(\d+)def/g.exec(s));//["hi↵abc454def", "4", index: 0, input: "hi↵abc454def"]  

试试 console.log(/h.+(\d+)def/g.exec(escape(s)));

.应该不包含\的,改下你的正则试试,

console.log(/h[.\\]+(\d+)def/g.exec(s));

点代表除了换行符以外的字符,\n正好是换行符

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题