Unable to group parts of the regex together to make the intended operator precedence explicit

The current regex /^(W)|([^a-zA-Z0-9_.]+)|(W)$/g is causing issues in SonarQube, and I need to group it properly. However, after grouping, it’s not working as expected.

The regex should meet the following conditions:

  1. Ensure there is a dot(s) at the start, end, or both ends of the string.
  2. In between, there can be anything except a-z, A-Z, 0-9, underscore, and dot (dots should not appear in the middle).

I tried this /^(.)|([^a-zA-Z0-9_.]+)|(.*.$)/g but i think it will again create an issue in SonarCube