I have an input which I am binding to keyup()
On each keyup, I want it to:
this.value = this.value.toLowerCase().replace(/[^0-9a-z-]/g,"");
$('.my-input').keyup(function() {
this.value = this.value.replace(/[^0-9a-zA-Z-]/g, '').toLowerCase();
});
Good question.. you're almost there!
$('.my-input').keyup(function() { this.value = this.value.replace(/[^A-Za-z0-9-]/g,"").toLowerCase();
Regex is not the right tool for lowercasing, use the built-in function. Your regex was good, but the replace function takes one regex and the replacement is a string, not a regex*.
(*replacement strings have some minor magic, but not enough for lowercasing)
The regex for a number, letter or dash is: [-0-9a-z]
(to include a literal dash in your character class, specify it as the first character; thereafter it's considered a range operator).
Try:
$('.my-input').keyup(function() {this.value = this.value.toLowerCase().replace(/[^-0-9a-z]/g,''); });