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

标签 javascript regex

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

例如;

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

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

相关文章:

javascript - Javascript 中两个数组的连接

javascript - Sprite 图像的背景位置在 IE 中无法用于倒计时时钟

python - Python 中的特殊字符模式匹配

javascript - 在 Javascript 中使用正则表达式屏蔽名称

java - 我可以在具有以下条件的 java 中为以下字符串获取适当的正则表达式吗

javascript - 使用react.js在其他组件内渲染组件

javascript - 在 c3 中有条件地添加或省略 JavaScript 属性

regex - 替换 ini 文件中的 block 文本

regex - Maven 替换插件,正则表达式不起作用

JavaScript 客户端宽度 : why is this returning undefined?