如果我们“展开”正则表达式,它们分别是:
- 替换
/\*\*\s*(.*?)\s*\*/
为** $1 **
;
- 替换
/\*.*?\*\*/
为.
;
- 替换
/\*[^*].*?[^*]\*/
为空(空字符串)。
第一个正则表达式:
/\*\* # Look for '/' followed by two occurrences of '*',
\s* # followed by zero or more space characters,
(.*?) # followed by zero or more of any character, lazily (with capture),
\s* # followed by zero or more space characters,
\*/ # followed by a '*' and a '/'
由于捕获中的量词是惰性的( ),这*?
意味着正则表达式引擎将在\s*
满足正则表达式的下一个组件(*
如果需要,在返回文本以满足下一个组件之前匹配最大可能的数量)。捕获$1
在替换文本中可用。
第二个正则表达式:
/\* # Look for a '/' then a '*',
.*? # followed by zero or more of any character, lazily,
\*\*/ # followed by two '*' and a '/'
并将所有这些替换为一个点 ( .
)。
第三个正则表达式:
/\* # Look for a '/' then a '*',
[^*] # followed by one character which is not a '*',
.*? # followed by zero or more of any character, lazily,
[^*] # followed by one character which is not a '*',
\*/ # followed by a '*' then a '/'.
并用空字符串替换所有这些。