Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于查找粗体文本的正则表达式_Javascript_Regex - Fatal编程技术网

Javascript 用于查找粗体文本的正则表达式

Javascript 用于查找粗体文本的正则表达式,javascript,regex,Javascript,Regex,我想获取**和**之间的文本 这就是我正在尝试的: /\**(.*)\**/ig 这是示例字符串 ** The ** *quick* ~~brown~~ fox ** jumped ** 请忽略**后和**前的空格** 因此,我获取了整个字符串我有点惊讶您的正则表达式没有因为编译问题而崩溃,因为星号是一个特殊的字符-它意味着以前的“零或更多”。根据您的正则表达式,您将捕获: * zero or more of (nothing) * zero or more of (nothing) (.)

我想获取
**
**
之间的文本

这就是我正在尝试的:

/\**(.*)\**/ig
这是示例字符串

** The ** *quick* ~~brown~~ fox ** jumped **
请忽略**后和**前的空格**


因此,我获取了整个字符串

我有点惊讶您的正则表达式没有因为编译问题而崩溃,因为星号是一个特殊的字符-它意味着以前的“零或更多”。根据您的正则表达式,您将捕获:

* zero or more of (nothing)
* zero or more of (nothing)
(.) any single character (in a capturing group)
* zero or more of (the previous item, 'any character')
我想你想要的是:

/\*\*(.*?)\*\*/ig
那就是:

\* the asterisk character
\* another asterisk
(.*?) anything, non-greedy (up until whatever is next in the regex)
\* the asterisk character
\* another asterisk

我有点惊讶你的正则表达式没有因为编译问题而崩溃,因为星号是一个特殊的字符——它意味着“零或更多”之前的任何字符。根据您的正则表达式,您将捕获:

* zero or more of (nothing)
* zero or more of (nothing)
(.) any single character (in a capturing group)
* zero or more of (the previous item, 'any character')
我想你想要的是:

/\*\*(.*?)\*\*/ig
那就是:

\* the asterisk character
\* another asterisk
(.*?) anything, non-greedy (up until whatever is next in the regex)
\* the asterisk character
\* another asterisk

尝试将
/\***(.*)\***/ig
替换为
/\***(.*)\***/ig
(因此在每个*前面都有一个反斜杠,因为您尝试两次获取文本*符号)是否尝试从标记文本中查找?,尝试此操作我想从字符串中获取**和**之间的文本(来自API响应)并加粗检测到的字符串并更新GUI。它是否已经加粗为降价格式?不,它不是!!!尝试将
/\***(.*)\***/ig
替换为
/\***(.*)\***/ig
(因此在每个*前面都有一个反斜杠,因为您尝试两次获取文本*符号)是否尝试从标记文本中查找?,尝试此操作我想从字符串中获取**和**之间的文本(来自API响应)并加粗检测到的字符串并更新GUI。它是否已经加粗为降价格式?不,它不是!!!它不会爆炸,因为
\**
意味着它将尝试匹配0个或多个星号字符。请记住,此答案还匹配两个以上星号之间的文本。例如,在字符串
“***三个星号之间的文本***”
中,它将匹配
“**三个星号之间的文本**”
,第1组为
“三个星号之间的文本”
@SilviuBurcea原始问题缺少一些格式,所以我看不到前导的反斜杠-现在它至少运行起来更合理了它不会爆炸,因为
\**
意味着它将尝试匹配0个或更多的星号字符。请记住,这个答案还匹配两个以上星号之间的文本。例如,在字符串
“***三个星号之间的文本***”
中,它将匹配
“**三个星号之间的文本**”
,第1组为
“三个星号之间的文本”
@SilviuBurcea原始问题缺少一些格式,所以我看不到最前面的反斜杠——这让我明白为什么它至少在运行