[英]how to use dotall flag for regex.exec()
i want get to string in a multiline string that content any specific character and i want get to between two specific staring.我想在包含任何特定字符的多行字符串中输入字符串,并且我想在两个特定的凝视之间进行。
i used this regex and this work but if content have any character (\\r \\n \\t) not work and get null value.我使用了这个正则表达式和这项工作,但如果内容有任何字符(\\r\\n\\t)不起作用并获得空值。
This Wotked
var regex = new RegExp("\-{2}Head(.*)-{2}\/\Head");
var content = "--Head any Code and String --/Head";
var match = regex.exec(content);
This Not Worked这不起作用
var regex = new RegExp("\-{2}Head(.*)-{2}\/\Head");
var content = "--Head \n any Code \n and String --/Head";
var match = regex.exec(content);
i found a regexer( http://www.regexr.com/v1/ ) and know i should use Dotall for multiline string but i cant use dotall for regex.exec我找到了一个正则表达式( http://www.regexr.com/v1/ )并且知道我应该将 Dotall 用于多行字符串,但我不能将 dotall 用于 regex.exec
thanks.谢谢。
In 2018, with the ECMA2018 standard implemented in some browsers for the time being, JS regex now supportss
DOTALL modifier : 2018年,随着ECMA2018标准暂时在部分浏览器中实现,JS正则表达式现在支持
s
DOTALL修饰符:
console.log("foo\\r\\nbar".match(/.+/s)) // => "foo\\r\\nbar"
Actually, JS native match-all-characters regex construct is实际上, JS 原生的 match-all-characters 正则表达式构造是
[^]
It means match any character that is not nothing .这意味着匹配任何不是 nothing 的字符。 Other regex flavors would produce a warning or an exception due to an incomplete character class ( demo ), though it will be totally valid for JavaScript ( demo ).
其他正则表达式会由于不完整的字符类 ( demo ) 产生警告或异常,尽管它对 JavaScript ( demo ) 完全有效。
The truth is, the [^]
is not portable, and thus is not recommendable unless you want your code to run on JS only.事实是,
[^]
不可移植,因此除非您希望代码仅在 JS 上运行,否则不推荐使用。
regex = /--Head([^]*)--\/Head/
To have the same pattern matching any characters in JS and, say, Java, you need to use a workaround illustrated in the other answers: use a character class with two opposite shorthand character classes when portability is key : [\\w\\W]
, [\\d\\D]
, [\\s\\S]
(most commonly used).要具有与 JS 和 Java 中的任何字符匹配的相同模式,您需要使用其他答案中说明的解决方法:当可移植性是关键时,使用具有两个相反速记字符类的字符类:
[\\w\\W]
, [\\d\\D]
, [\\s\\S]
(最常用)。
NOTE that [^]
is shorter .注意
[^]
更短。
javascript doesn't support s
(dotall) modifier. javascript 不支持
s
(dotall) 修饰符。 The only workaround is to use a "catch all" class, like [\\s\\S]
instead of a dot:唯一的解决方法是使用“catch all”类,例如
[\\s\\S]
而不是点:
regex = new RegExp("\-{2}Head([\\s\\S]*)-{2}\/\Head")
Also note that your expression can be written more concisely using a literal:另请注意,您的表达式可以使用文字更简洁地编写:
regex = /--Head([\s\S]*)--\/Head/
Use catch all character class [\\s\\S]
which means space or non space使用捕获所有字符类
[\\s\\S]
表示空格或非空格
var regex = new RegExp("\-{2}Head([\s\S]*)-{2}\/\Head","m");
var content = "--Head \n any Code \n and String --/Head";
var match = regex.exec(content);
You are looking for the s
modifier, also known as the dotall modifier which forces the dot .
您正在寻找
s
修饰符,也称为强制 dot 的dotall修饰符.
to also match newlines.也匹配换行符。 The dotall modifier does not exist in javascript .
javascript中不存在 dotall 修饰符。 The workaround is replacing the dot
.
解决方法是替换点
.
with...和...
[\S\s]*
Your regular expression would look like this.你的正则表达式看起来像这样。
var regex = /-{2}Head([\S\s]*)-{2}\/Head/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.