I am struggling with capturing and non-capturing groups in Javascript.
I have the following string
var string = "#Blah blah blah\n#ENV:This is env note 1\n#MISC:this is a misc note\n#ENV:this is env note 2\n\n";
I would like to capture each text that starting with #ENV: and ending with new line, without capturing the '#ENV:' tag itself. (ie I want to return ["this is env note 1", "this is env note 2"]
I have tried the following:
var capture = string.match(/(?:#ENV)([^\n]*)/gi);
// Returns: ["#Blah blah blah", "#ENV:This is env note 1", "#MISC:this is a misc note", "#ENV:this is env note 2"]
var capture2 = string.match(/(?:(#ENV))([^\n]*)/gi);
//Returns ["#ENV:This is env note 1", "#ENV:this is env note 2"]
Any advice is greatly appreciated.
Thanks
Leo
两个正则表达式具有相同的结果,您必须从结果中剪切#ENV:
Found solution.
Problem comes from match
method.
The following code works instead
var regex = /#ENV:(.*?)\n/gi;
var match;
var result=[];
while(match = regex.exec(string)) {result.push(match[1]);}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.