I want to match the part of a regular expression that spans multiple lines in parentheses.

Asked 2 years ago, Updated 2 years ago, 71 views

In javascript, I would like to get a regular expression of the sentence ending with "." in the part that is not surrounded by "to" from the text below.

 Ayeo
[
AIUEO。
kakikukeko。
]
Hi, everyone.
goodbye。
hello

The method I tried was to exclude the regular expression pattern that matches in [ ] with a negative look-ahead, and the result was
in [ ]. Oh, my. Kakikakeko.
It also matches the .

/^(?!\[[\s\S]*?\].*?$/gm

How do I write to satisfy my purpose?

javascript regular-expression

2022-09-30 17:47

2 Answers

The flag is m, so ^$ matches the beginning and end of the line.
So the question /^(?!\[[\s\S]*?\).*?$/gm hits all lines that do not have at the end of the line, regardless of the negative look-ahead..
For example, if you rewrite to , the line will also be a hit.

It is difficult to determine . at each end of each line while excluding [].
Like @cubick's comment, I think the proper way to write is to first exclude parentheses without using the m flag and then use the m flag to get matches for each line.

vars="Aiueo\r\n"+
US>"[\r\n" +
US>"Aye.\r\n" +
US>"Scratch.\r\n" +
US>" ]\r\n" +
"Hello,\r\n"+
US>"Goodbye\r\n"
"Hello";
r=s.replace(/^([\s\S])*)\[[\s\S]*\]([\s\S]*)$/, "$1$2");
m=r.match(/^.+.$/gm);
console.log(m);


2022-09-30 17:47

Is it like this?

vars= 
Nice to meet you.\n"+
US>"Aye\n"+
US>"[\n"+
US>"Aye.\n"
US>"Kakikakeko\n"+
US>"]\n" +
"Hello,\n"+
US>"[\n"+
"Stand up.\n" +
US>"]\n" +
US>"Goodbye\n"
"Hello";
var regex=/(?<![^\]]*)^[^\[\]]*.$/gm;
var match=s.match(regex);
console.log(match);

"After [, ] must not appear" is a negative look-ahead.

In a simple example, I think it works (although Safari didn't do a negative look-ahead…), but I think it would be easier to understand and better to say [ and ].


2022-09-30 17:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.