如何使用dotall标志为regex.exec() [英] how to use dotall flag for regex.exec()

查看:116
本文介绍了如何使用dotall标志为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.

我使用了这个正则表达式和这个工作但是如果内容有任何字符(\r \ n \t)不起作用并获得空值。

i used this regex and this work but if content have any character (\r \n \t) not work and get null value.

This Wotked
    var regex = new RegExp("\-{2}Head(.*)-{2}\/\Head");      
    var content = "--Head any Code and String --/Head";
    var match = regex.exec(content);

这不工作

var regex = new RegExp("\-{2}Head(.*)-{2}\/\Head");      
var content = "--Head \n any Code \n and String --/Head";
var match = regex.exec(content);

我找到了一个regexer( http://www.regexr.com/v1/ )并知道我应该使用Dotall作为多行字符串,但我不能使用dotall for regex.exec

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

谢谢。

推荐答案

javascript不支持 s (dotall)修饰符。唯一的解决方法是使用全部捕获类,如 [\\\ S] 而不是点:

javascript doesn't support s (dotall) modifier. The only workaround is to use a "catch all" class, like [\s\S] instead of a dot:

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/

这篇关于如何使用dotall标志为regex.exec()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆