Refining my javascript REGEX to format a short-name
I am writing a javascript regex to turn a string into a short-name (for use in clean URLs). I need it to:
- convert it to lowercase
- strip all "bad" characters
- trim all leading and trailing whitespace
- replace single and multiple spaces for a dash.
This is what I have so far (newlines added for readability):
clean_value = $(this).val().toLowerCase().replace(/[^a-z0-9 -]+/g,'')
.replace(/^\s+|\s+$/g, ''开发者_如何学C).replace(/ +(?= )/g,'-')
.replace(/ /g, '-');
This does it but I KNOW there is a more efficient way to form these REGEXs... I am so bad with them and have spent an hour trying to figure out how to combine these as much as possible.
Any help would be greatly appreciated!
This doesn't look too bad in my opinion. Better have a sequence of readable, manageable regexes than one all-purpose monster that nobody (including yourself) will understand a few weeks from now.
One suggestion:
clean_value = $(this).val().toLowerCase().replace(/[^a-z0-9 -]+/g,'')
.replace(/^\s+|\s+$/g, '').replace(/ +/g,'-');
to make the last replace()
do what you specified - convert one or more spaces into a single dash.
If you still have multiple dashes in a row that you want to reduce to a single one, you could add a
.replace(/-{2,}/, '-')
at the end.
The part where you trim the whitespace is actually faster if you split it into two separate replaces. See: Faster JavaScript Trim. Here's how I would recommend doing it:
function clean(text) {
return text.replace(/^\s+/,'').replace(/\s\s*$/,'').toLowerCase()
.replace(/[^a-z0-9\-\s]+/g,'').replace(/\s+/g,'-');
}
Note that the order is important to avoid multiple dashes.
精彩评论