Regular expression in javascript to remove anything that is not: az 0-9 and hyphen

Thanks in advance.

I would like the regex to remove anything that is not the letter α and the hyphen. So allowed are AZ 0-9 and -.

Also, how can I apply this to a string in Javascript?

Thanks again.

+3
source share
5 answers
var str = 'a23a-asd!@#$';
str.replace(/[^-a-z0-9]/ig,'');
+8
source

Try the following:

str = str.replace(/[a-zA-Z\d-]/g, "");
+1
source
var output = input.replace(/[^A-Za-z0-9-]/g, "");
0

string.replace(/[^ a-zA-Z0-9 -]/g, "");

0
$(document).ready(function (e) {
        $('#Box, #Window').keyup(function(){
            var $th = $(this);
            $th.val($th.val().replace(/[^a-zA-Z0-9]/g, function(str){return '';}));
        });

    });

- .

0

Source: https://habr.com/ru/post/1718714/


All Articles