gpt4 book ai didi

Javascript 正则表达式匹配由一对字符括起来的项目?

转载 作者:行者123 更新时间:2023-11-29 17:34:09 25 4
gpt4 key购买 nike

我想要一个正则表达式来匹配字符 (**) 之间的所有内容。我还需要在结果中匹配 (**)

例如;

这样的正则表达式应用于下面的代码;

(*
* 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

Updated RegEx Demo

正则表达式详细信息:

  • \(\*:匹配起始 (*
  • [\s\S]*?:匹配0个或多个任意字符,包括换行符(非贪婪)
  • \*\):匹配结束*)

关于Javascript 正则表达式匹配由一对字符括起来的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58841905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com