Looking to create a regular expression for a string (company/organization name) with the following conditions:
- No leading or trailing spaces
- No double spaces in between
- Shouldn't allow only a single character (alphanumeric or whitelisted)
- Can start with an alphanumeric or whitelisted character
- Shouldn't allow any whitelisted character to be entered multiple times
Here is the regex for these conditions:
/(?! )([a-zA-Z0-9_\.\-#&])+([a-zA-Z0-9_\.\-#&\s])*(?<! )$/
console.log(/(?! )([a-zA-Z0-9_\.\-#&])+([a-zA-Z0-9_\.\-#&\s])*(?<! )$/.test('_')); // shouldn't allow
console.log(/(?! )([a-zA-Z0-9_\.\-#&])+([a-zA-Z0-9_\.\-#&\s])*(?<! )$/.test('a')); // shouldn't allow
console.log(/(?! )([a-zA-Z0-9_\.\-#&])+([a-zA-Z0-9_\.\-#&\s])*(?<! )$/.test('abc abc')); // shouldn't allow
console.log(/(?! )([a-zA-Z0-9_\.\-#&])+([a-zA-Z0-9_\.\-#&\s])*(?<! )$/.test('_123')); // works fine
console.log(/(?! )([a-zA-Z0-9_\.\-#&])+([a-zA-Z0-9_\.\-#&\s])*(?<! )$/.test('# abc')); // works fine
console.log(/(?! )([a-zA-Z0-9_\.\-#&])+([a-zA-Z0-9_\.\-#&\s])*(?<! )$/.test('abc abc!')); // works fine
console.log(/(?! )([a-zA-Z0-9_\.\-#&])+([a-zA-Z0-9_\.\-#&\s])*(?<! )$/.test('abc abc# abc')); // works fine
The current regex does not meet all the criteria and it is unclear what the issue with the regex is.