alexander farkas alexander farkas - 5 months ago 8x
Javascript Question

Match any character after a dot until either next dot or end of line

As my question I want to match

out of
asa .foo. assas
and in case of
bar .foo

While I know how to do one of these ending conditions, I don't know how to combine them.

My current work looks something like this:



I would suggest the following:


How it works:

\.    # . (Dot)
(     # Capture Data (foo)
  \S    # Any Non-Whitespace Character
  *     # 0 or more times
(?=   # Lookahead (Check) for...
  \.    # . (Dot)
  |     # OR
  $     # End of String

The data you want is stored in the 1st Capture Group

Live Demo on Regex101

Problems with your RegEx:

  • The \. is fine
  • .+? makes no sense.
    • + means 1 or more times, whereas * means 0 or more times. So I can see what you were trying to get at, but it is simpler to just use (.*)
  • [\.|\Z] will not work as you expected.
    • First, no need to escape the \., just use .. Next, any character inside [] will be matched. This means it will match ., | or \Z. You only use | when inside a Group, like \.|\Z. Finally, you can use $ instead of \Z
    • This means can (?=\.|$) or (?=[.\Z]) (\Z needs to be used here because $ matches a $ literally)