This simple regex is troubling me. Can you lend a hand?
How can I get Foo's Bar to return foos-bar in javascript?
var str = "Foo's Bar";
str.replace(/\s+/g, '-').replace('/[^a-zA-Z-]/g', '').toLowerCase();
return str;
The best I can do is foo's-bar, leaving the '.
Thanks.
Esailija is spot on – your regex is correct, but the second one is being interpreted as a string since it’s wrapped in quotes. With that fixed, your code works perfectly: