我希望我的代码查找包含至少一个字符和换行符(零个或多个)的字符串。
[\s\S]+
不适用于我,因为它也只匹配换行符(没有任何字符)。
您正在寻找的模式是
/\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' ]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句