字符串正则表达式 - 包括和endsWith

时间:2016-07-05 21:02:56

标签: javascript regex

我正在尝试编写一些模拟JavaScript中includesendsWith函数的正则表达式。我有一个字符串(“ted”)。我想看看是否包含该字符串或使用正则表达式在我的任何其他字符串的末尾。

我看到一些\b的例子,我以前从未见过。这让我想知道RegEx是否有一些增强功能,我无法找到任何相关信息。

1 个答案:

答案 0 :(得分:0)

描述

(?:(?=(?:(for)$)?)(for))

Regular expression visualization

**要更好地查看图像,只需右键单击图像并在新窗口中选择视图

此正则表达式将执行以下操作:

  • 模拟包含和结束功能
  • 如果字符串包含子字符串for,则将填充捕获组2。
  • 如果字符串以for结尾,则将填充捕获组1。

注意:

  • 在Javascript中,您可以运行匹配,然后使用匹配的.index属性返回匹配的字符位置。
  • 这是正则表达式,如果子字符串位于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: ')'
----------------------------------------------------------------------
  ;                        ';'
----------------------------------------------------------------------
相关问题