-
-
Save m4olivei/75b88acd2c647d21f37d42396c468ddc to your computer and use it in GitHub Desktop.
Clean CSS identifier from string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In a perfect world, you'd never have to do this. Wish I lived in a perfect world. | |
/** | |
* Convert a string to a usable class name | |
* @param {string} stringToConvert String of text | |
* @return {string} Usable class name | |
*/ | |
function convertStringToClassName(stringToConvert) { | |
if (typeof stringToConvert === 'string') { | |
return stringToConvert.trim().replace(/[\s-_]+/g, '-').replace(/[^\w-]|^-|-$/g, '').replace(/-+(?=-)/, '').toLowerCase(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@wesruv this will also remove leading and trailing "-"'s and remove consecutive dashes, which may have been left after the second replace removes a sequence of all non-word characters between two dashes, such as an input like: