I am currently dealing with a regex issue in SonarQube that requires grouping to clarify operator precedence. Despite grouping the regex, it is not functioning as anticipated.
SonarQube Concern: SonarQube indicates that the regex should have grouped components to ensure clear operator precedence.
Existing Regex: /^(\W)|([^a-zA-Z0-9_.]+)|(\W)$/g This regex is designed to validate a string based on specific criteria:
Criteria:
- If the string begins or ends with dots, an error should be triggered immediately.
- If the string contains any symbols other than A-Z, a-z, 0-9, underscore, or dot (with dots only permitted in between), an error should be raised.
- The string should consist solely of A-Z, a-z, 0-9, underscore, or dots (dots cannot start or end but can be present in between).
Note: The current logic is set up to generate an error if the regex matches. Hence, I require a regex that contradicts the mentioned conditions without altering the existing logic, which is integral to a reusable codebase.
I tried using the regex /^(.)|([^a-zA-Z0-9_.]+)|(.*.$)/g, but I am apprehensive about potential SonarQube issues due to operator precedence.
How can I properly format this regex to fulfill these requirements and prevent SonarQube alerts?