我正在尝试编写一些模拟JavaScript中includes
和endsWith
函数的正则表达式。我有一个字符串(“ted”)。我想看看是否包含该字符串或使用正则表达式在我的任何其他字符串的末尾。
我看到一些\b
的例子,我以前从未见过。这让我想知道RegEx是否有一些增强功能,我无法找到任何相关信息。
答案 0 :(得分:0)
(?:(?=(?:(for)$)?)(for))
**要更好地查看图像,只需右键单击图像并在新窗口中选择视图
此正则表达式将执行以下操作:
for
,则将填充捕获组2。for
结尾,则将填充捕获组1。注意:
for
之类的其他单词内,子字符串before
也将匹配,但这似乎超出了当前问题的范围。现场演示
https://regex101.com/r/bO5vO3/1
示例文字
Are these the droids for we for are looking for? These are not the droids you are looking for
Match: ^1^ ^2^ ^3^ ^4^
显示位置的第二行是为了说明目的手动插入的,不应被视为实际示例文本的一部分。
样本匹配
MATCH 1
2. `for`
MATCH 2
2. `for`
MATCH 3
2. `for`
MATCH 4
1. `for`
2. `for`
NODE EXPLANATION
----------------------------------------------------------------------
[\$] any character of: '\$'
----------------------------------------------------------------------
this-translate 'this-translate'
----------------------------------------------------------------------
[(] any character of: '('
----------------------------------------------------------------------
' '\''
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
[^'\\] any character except: ''', '\\'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\\ '\'
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
'' '\'\''
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
' '\''
----------------------------------------------------------------------
[)] any character of: ')'
----------------------------------------------------------------------
; ';'
----------------------------------------------------------------------