I have the following code that fills a field when entering into another ...
$('#title').bind('keyup keypress blur', function() { var myStr = $(this).val() myStr=myStr.toLowerCase(); myStr=myStr.replace(/ /g,"-"); myStr=myStr.replace(/[^a-zA-Z0-9\.]+/g,""); myStr=myStr.replace(/\.+/g, "-"); $('#filename').val(myStr); });
Removing any alphanumeric characters in the code works, however I want them to replace the spaces with the "-" character.
If I change the dash to the period '.' it works, donβt know why and how to fix it? Regex bothers me.
JSFIDDLE here
Can someone point me in the right direction?
UPDATE
It just worked as required, using this ...
<script> $('#title').bind('keyup keypress blur', function() { var myStr = $(this).val() myStr=myStr.toLowerCase(); myStr=myStr.replace(/[^a-zA-Z0-9 ]+/g,""); myStr=myStr.replace(/\s+/g, "-"); $('#filename').val(myStr); }); </script>
Dijon source share