-2

I have the following code:

$('.pure').on('keyup', function (e) {            
    e.preventDefault();
    var str = $(this).val();
    $(this).val(str.replace(/[^a-z0-9\s,.-^%]+$/ig, ''));
});

I want to only allow for the following characters:

A-Z a-z 0-9 , . - % and space. 

My string that I am entering in the input box:

You have selected the following date - %date%. 

Results:

The % keeps being removed from the string. Other special characters are also being removed $& etc. Which is correct. But I do not want the % to be removed.

Sickaaron
  • 448
  • 1
  • 12
  • 21

1 Answers1

0

Move - to the end of the regular expression character class so it is not interpreted as a range.

str.replace(/[^a-z0-9\s,.%-]/ig, '')
Unmitigated
  • 76,500
  • 11
  • 62
  • 80