Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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,如果字符串包含两个*s而不是一个*,我希望正则表达式忽略该字符串: **hello** *hello* 我的正则表达式捕获两个*hello*s。我如何才能只捕获*hello*而不捕获**hello**? 以下是我当前的正则表达式: \*{1}?[^\*|\s]{1,}?\*{1}? 编辑:我正在尝试做的: 我正在制作一个Chrome扩展,它将*hello*转换为hello,并将**hello**转换为hello。但是,由于正则表达式使**hello**与*hello*重叠,因此它将粗体转换为

如果字符串包含两个*s而不是一个*,我希望正则表达式忽略该字符串:

**hello**
*hello*
我的正则表达式捕获两个
*hello*
s。我如何才能只捕获
*hello*
而不捕获
**hello**
? 以下是我当前的正则表达式:

\*{1}?[^\*|\s]{1,}?\*{1}?
编辑:我正在尝试做的:

我正在制作一个Chrome扩展,它将
*hello*
转换为hello,并将
**hello**
转换为hello。但是,由于正则表达式使
**hello**
*hello*
重叠,因此它将粗体转换为斜体。我怎样才能停止呢?

这样做的关键是用
\1
替换
\*\*([^*]*)\*\*
,然后用
\1
替换
\*([^*]*)\*
,或者用strong/em、css样式替换
\*([^*]*)\*
,无论您实际使用什么来更改字体


要覆盖嵌套在粗体标记中的i(反面是不存在问题的),您可以将其用于粗体化正则表达式:
\*\*([^*]*(\*[^*]*\*[^*]*)\*\*

,您可以使用以下命令:

[^*]\*([^\*\s]+)\*($|[^*])
参见

您不需要正则表达式来表示“如果它包含”;试试这个:

var contains = "**hello**".indexOf("**")>=0;
// 'contains' is true if '**' exists in the string.

那么
**你好*
*你好**
呢?这不是问题。我能对付他们。他们捕捉的不是错误的。
但是应该捕捉什么呢
hi*hello*
?错误地按了enter键-我正在尝试使用
**hello**
将文本转换为粗体,并将
*hello*
转换为斜体。相反,它将粗体变成斜体,因为它们重叠。为什么不先搜索
**hello**
,然后搜索
*hello*
?如此接近!我希望两个“h”之间的
*hello*
匹配。基本上先匹配所有特定的表达式,然后保留最通用的(将匹配多个字符串)作为最后一个。我希望正则表达式能更高效一些,但我接受这一点。我确实想过这样做,但这并不是我想要的。