Javascript RegExp问题的条件表达式
玩Regex很开心,我想知道是否有一种方法可以基于某些标准在regexp中设置条件-希望返回a或B与B或a之间的值-但在我的Regex 101中,由于以下字符串,它似乎不起作用:Javascript RegExp问题的条件表达式,javascript,regex,Javascript,Regex,玩Regex很开心,我想知道是否有一种方法可以基于某些标准在regexp中设置条件-希望返回a或B与B或a之间的值-但在我的Regex 101中,由于以下字符串,它似乎不起作用: (?<=A|B\s+).*?(?=\s+B|A) //<-hopefully should return anything between or an A or a B is this possible? A adawdawdawwad B awdawdawdda A awdaddawdaw
(?<=A|B\s+).*?(?=\s+B|A) //<-hopefully should return anything between or an A or a B is this possible?
A
adawdawdawwad
B
awdawdawdda
A
awdaddawdaw
B
awdadadaw
B
awdadadaw
A
adadawaw
B
awdawdwadawd
A
(?最简单的一个
A[^B]+B|B[^A]+A
您可以使用正则表达式
(?<=^ *A *\r?\n)(?:(?!^ *B *?$).*\r?\n)*(?=^ *B *?$)|(?<=^ *B *\r?\n)(?:(?!^ *A\s*$).*\r?\n)*(?=^ *A *?$)
在当前模式中,(?我相信你缺少括号(?@Lucas,如果你的意思是字符串“我的狗有跳蚤”
不是从“艾米的狗有跳蚤”
中提取的,我会同意。我在“Javascript演示”链接后面的句子中给出了我对这个问题的理解,它不包括字符串,例如“AMy dog has flesb”
。
(?<= # begin positive lookbehind
^ *A\ *\r?\n # match 0+ spaces, 'A', 0+ spaces, opt CR,
# newline, from beginning of line
) # end positive lookbehind
(?: # begin non-capture group
(?! # begin negative lookahead
^ *B *$ # match 0+ spaces, 'B', 0+ spaces, end of
# line, from beginning of line
) # end negative lookahead
.*\r?\n # match 0+ chars, opt CR, newline
) # end non-capture group
* # execute non-capture group 0+ times
(?= # begin positive lookahead
^ *B *?$ # match 0+ spaces, 'B', 0+ spaces, end of
# line, from beginning of line
) # end positive lookahead
| # or
(?<= # begin positive lookbehind
^ *B\ *\r?\n # match 0+ spaces, 'B', 0+ spaces, opt CR,
# newline, from beginning of line
) # end positive lookbehind
(?: # begin non-capture group
(?! # begin negative lookahead
^ *A *$ # match 0+ spaces, 'A', 0+ spaces, end of
# line, from beginning of line
) # end negative lookahead
.*\r?\n # match 0+ chars, opt CR, newline
) # end non-capture group
* # execute non-capture group 0+ times
(?= # begin positive lookahead
^ *A *?$ # match 0+ spaces, 'A', 0+ spaces, end of
# line, from beginning of line
) # end positive lookahead
(?<=[AB]\s+).*?(?=\s+[AB])