javascript - Javascript正则表达式匹配由一对字符包围的项目?
问题描述
我想要一个正则表达式来匹配字符(*
和*)
. 我也需要(*
并*)
在结果中匹配。
例如;
这样的正则表达式适用于下面的代码;
(*
* 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 个或多个任意字符,包括换行符(非贪婪)\*\)
: 比赛结束*)
推荐阅读
- java - “Apache HTTP Server”中的配置“Content-Disposition 标头”
- c# - 管理谁连接到 ClickOnce 应用程序
- python - 如何从列表中更新 matplotlib 动画标题?
- javascript - 使用 Algolia places.js 库进行反向地理编码查询
- python - 在使用 Python 的 MySQL 查询中使用 %s 进行字符串格式化时出错
- javascript - 使用 javascript 显式关闭 xml 标签
- xamarin.forms - GPS开启后需要使用xamarin表单获取当前位置
- php - 使用 PHP 的 JSON 到 HTML 行
- perl - 另一种设置 DB::deep 的方法?
- android - 懒惰的 Android 依赖项。如何在 gradle 依赖解析之前构建 AARs 库