我想要一个正则表达式来匹配字符 (*
和 *)
之间的所有内容。我还需要在结果中匹配 (*
和 *)
。
例如;
这样的正则表达式应用于下面的代码;
(*
* Something here
* Many things can appear here
* More things can appear here
* Added another can appear here
*)
(*****************************************) Something here
Something here (*****************************************)
(* Content can also exist here *) Something here
Something here (* Content can also exist here *)
(Some content here ) (* Content can also exist here *)
* Something here
* Many things can appear here
* More things can appear here
* Added another can appear here
结果只包含;
(*
* Something here
* Many things can appear here
* More things can appear here
* Added another can appear here
*)
(*****************************************)
(*****************************************)
(* Content can also exist here *)
(* Content can also exist here *)
(* Content can also exist here *)
我有事发生 https://regexr.com/4nset
([\(\*].*(?:\*\)))
但它似乎没有按预期工作。
最佳答案
假设没有嵌套或转义括号,您可以在 Javascript 中使用此正则表达式:
/\(\*[\s\S]*?\*\)/g
正则表达式详细信息:
\(\*
:匹配起始(*
[\s\S]*?
:匹配0个或多个任意字符,包括换行符(非贪婪)\*\)
:匹配结束*)
关于Javascript 正则表达式匹配由一对字符括起来的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58841905/