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

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));

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"]  

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


http://stackoverflow.com/a/1981692/2586541


js正则不支持单行模式

console.log(/h(.|\n)+(\d+)def/g.exec(s));

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


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

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

【热门文章】
【热门文章】