2014-01-11 70 views

回答

0

你正在寻找的模式是

/\w+\n*/ 

这任何字母数字字符和_ 1次或更多次,然后换行字符0次或更多次

见少数的例子匹配

var pattern = /\w+\n*/ 
console.log("a".match(pattern)); 
console.log("aaa".match(pattern)); 
console.log("a\n".match(pattern)); 
console.log("a\n\n\n".match(pattern)); 
console.log("a\nb".match(pattern)); 
console.log("\na\nb".match(pattern)); 

输出

[ 'a', index: 0, input: 'a' ] 
[ 'aaa', index: 0, input: 'aaa' ] 
[ 'a\n', index: 0, input: 'a\n' ] 
[ 'a\n\n\n', index: 0, input: 'a\n\n\n' ] 
[ 'a\n', index: 0, input: 'a\nb' ] 
[ 'a\n', index: 1, input: '\na\nb' ] 
+0

这不符合“?\ n”。 – usr2564301

+0

@Jongware Yup因为字符的定义是模棱两可的我认为 – thefourtheye

+0

但它不适用于'console.log(“a \ nb”.match(pattern));''或'console.log(“\ na \ nb “.match(pattern));' 这就是我想要做的! – mmaiiladsf

相关问题