Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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,作为我的问题,我想在asa.foo中匹配foo。ASAS和在bar.foo的情况下 虽然我知道如何执行这些结束条件之一,但我不知道如何组合它们 我目前的工作是这样的: /\.(.+?)(?=[\.|\Z])/g 您可以使用此正则表达式捕获第一个点后的下一个非点文本: /^[^.]*\.([^.]*)/ 您的文本在捕获的组#1中可用 正则表达式分解: ^ # line start [^.]* # match 0 or more chars that are not

作为我的问题,我想在
asa.foo中匹配
foo
。ASAS
和在
bar.foo
的情况下

虽然我知道如何执行这些结束条件之一,但我不知道如何组合它们

我目前的工作是这样的:

/\.(.+?)(?=[\.|\Z])/g

您可以使用此正则表达式捕获第一个点后的下一个非点文本:

/^[^.]*\.([^.]*)/
您的文本在捕获的组#1中可用

正则表达式分解:

^          # line start
[^.]*      # match 0 or more chars that are not DOT
\.         # match a literal DOT
([^.]*)    # match 0 or more chars that are not DOT and capture the text in group #1
\.(.+?)(?=[\.|\Z])
代码:

^          # line start
[^.]*      # match 0 or more chars that are not DOT
\.         # match a literal DOT
([^.]*)    # match 0 or more chars that are not DOT and capture the text in group #1
\.(.+?)(?=[\.|\Z])
var re=/^[^.]*\.([^.]*)/gm;
var str='asa.foo。阿萨斯\ nbar.foo';
var-m;
var匹配=[];
while((m=re.exec(str))!==null){
如果(m.index==re.lastIndex)
re.lastIndex++;
matches.push(m[1]);
}

书面文件(匹配项)您可以使用此正则表达式捕获第一个点后的下一个非点文本:

/^[^.]*\.([^.]*)/
您的文本在捕获的组#1中可用

正则表达式分解:

^          # line start
[^.]*      # match 0 or more chars that are not DOT
\.         # match a literal DOT
([^.]*)    # match 0 or more chars that are not DOT and capture the text in group #1
\.(.+?)(?=[\.|\Z])
代码:

^          # line start
[^.]*      # match 0 or more chars that are not DOT
\.         # match a literal DOT
([^.]*)    # match 0 or more chars that are not DOT and capture the text in group #1
\.(.+?)(?=[\.|\Z])
var re=/^[^.]*\.([^.]*)/gm;
var str='asa.foo。阿萨斯\ nbar.foo';
var-m;
var匹配=[];
while((m=re.exec(str))!==null){
如果(m.index==re.lastIndex)
re.lastIndex++;
matches.push(m[1]);
}

书面文件(匹配项)我建议如下:

\.(\S*)(?=\.|$)
工作原理:

\.    # . (Dot)
(     # Capture Data (foo)
  \S    # Any Non-Whitespace Character
  *     # 0 or more times
)
(?=   # Lookahead (Check) for...
  \.    # . (Dot)
  |     # OR
  $     # End of String
)
所需数据存储在
1st
捕获组中

您可能希望将其更改为:

.(\S+)(?=.|$)

阅读@Паааааааааааааа


正则表达式的问题:

^          # line start
[^.]*      # match 0 or more chars that are not DOT
\.         # match a literal DOT
([^.]*)    # match 0 or more chars that are not DOT and capture the text in group #1
\.(.+?)(?=[\.|\Z])
  • \.
    很好
  • +?
    毫无意义(实际上,请参考@ПааааПаааааааааааа。
    • +
      表示1或更多次,而
      *
      表示0或更多次。因此,我可以看到您试图得到什么,但只使用
      (.*)
      (除非字符串中有多个
      ,并且您希望它是非贪婪的。再次,请参阅@Паааааааааааааааа
  • [\.\124;\ Z]
    将无法按预期工作。
    • 首先,不需要逃避
      \。
      ,只需使用
      。接下来,将匹配
      []
      中的任何字符。这意味着它将匹配
      \Z
      。您仅在组内使用
      ,如
      \.\Z
      。最后,您可以使用
      $
      而不是
      \Z
    • 这意味着可以
      (?=\.\124;$)
      (?=[.\Z])
      \Z
      需要在这里使用,因为
      $
      $
      字面上匹配)

    • 我建议如下:

      \.(\S*)(?=\.|$)
      
      工作原理:

      \.    # . (Dot)
      (     # Capture Data (foo)
        \S    # Any Non-Whitespace Character
        *     # 0 or more times
      )
      (?=   # Lookahead (Check) for...
        \.    # . (Dot)
        |     # OR
        $     # End of String
      )
      
      所需数据存储在
      1st
      捕获组中

      您可能希望将其更改为:

      .(\S+)(?=.|$)

      阅读@Паааааааааааааа


      正则表达式的问题:

      ^          # line start
      [^.]*      # match 0 or more chars that are not DOT
      \.         # match a literal DOT
      ([^.]*)    # match 0 or more chars that are not DOT and capture the text in group #1
      
      \.(.+?)(?=[\.|\Z])
      
      • \.
        很好
      • +?
        毫无意义(实际上,请参考@ПааааПаааааааааааа。
        • +
          表示1或更多次,而
          *
          表示0或更多次。因此,我可以看到您试图得到什么,但只使用
          (.*)
          (除非字符串中有多个
          ,并且您希望它是非贪婪的。再次,请参阅@Паааааааааааааааа
      • [\.\124;\ Z]
        将无法按预期工作。
        • 首先,不需要逃避
          \。
          ,只需使用
          。接下来,将匹配
          []
          中的任何字符。这意味着它将匹配
          \Z
          。您仅在组内使用
          ,如
          \.\Z
          。最后,您可以使用
          $
          而不是
          \Z
        • 这意味着可以
          (?=\.\124;$)
          (?=[.\Z])
          \Z
          需要在这里使用,因为
          $
          $
          字面上匹配)

      我会使用这样的东西:
      /\(\S+?)(?=\。\124;$)/g

      \.
      文字点
      (\S+?)
      一个或多个非空白字符(非贪婪)
      (?=\.\124;$)
      点或行尾的正向前瞻


      我会使用这样的东西:
      /\.(\S+?)(?=\.\124;$)/g

      \.
      文字点
      (\S+?)
      一个或多个非空白字符(非贪婪)
      (?=\.\124;$)
      点或行尾的正向前瞻


      以下模式应适用:

      \.(\w+)(?:\.\s+\S+)?$
      

      以下模式应适用:

      \.(\w+)(?:\.\s+\S+)?$
      

      在这种情况下,我会反对正则表达式

      它可以用更易读一点的方式来完成

      'asa .foo. assas'.split('.')[1]
      

      在这种情况下,我会反对正则表达式

      它可以用更易读一点的方式来完成

      'asa .foo. assas'.split('.')[1]
      

      你确定你指的是任何字符(包括空格)?你确定你指的是任何字符(包括空格)?实际上,
      +?
      是有意义的:问号使量词不贪婪,因此它将匹配尽可能少的字符。这就是为什么你的解决方案不能处理超过两个点的字符串,比如
      'a.b.c.d'
      。实际上,
      +?
      是有意义的:问号使量词不贪婪,因此它将匹配尽可能少的字符。这就是为什么你的解决方案不能处理超过两个点的字符串,比如
      'a.b.c.d'