简体   繁体   English

Javascript正则表达式:如何匹配整个文本?

[英]Javascript regex: how to match on the whole text?

I can't seem to find a way for a javascript regex to match on the whole text, and not line by line. 我似乎无法找到一种方法让javascript正则表达式匹配整个文本,而不是逐行。 I'd like that the regex engine do not regard '\\n' as the end of the text, but as a character like any other. 我希望正则表达式引擎不会将'\\ n'视为文本的结尾,而是将其视为任何其他角色。

Here's an example of the problem: 这是一个问题的例子:

var string = "Aaa\nbb";
console.log(/^A(.*)/g.exec(string)[1]);

Result in the console: 控制台中的结果:

aa

. does not match line breaks and unfortunately JavaScript does not have a s switch to make it match line breaks as well. 与换行符不匹配,遗憾的是JavaScript没有s开关使其匹配换行符。 So you can't use . 所以你不能使用. or only together with [\\n\\r] like: 或者只与[\\n\\r]一起使用:

/(.|[\n\r])+/

It would be simpler to use an expression other than . 使用除以外的表达式会更简单. like [^] , which will match any character . 比如[^] ,它将匹配任何角色 So: 所以:

/[^]+/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM