The script removes characters from the list from a string.

function withoutCyr(input) { var value = input.value; var re = /а|б|в|г|д|е|ё|ж|з|и|ё|к|л|м|н|о|п|р|с|т|у|ф|х|ц|ч|ш|щ|ъ|ы|ь|э|ю|я|'|"|\?|<|>|\/|\\|:|;|!|#|%|\]|\[|&|^|\*|$|!|\+| /gi; if (re.test(value)) { value = value.replace(re, ''); input.value = value; } } 

The + sign is removed only the 2nd and subsequent ones, the 1st is not deleted.

  • What does the second and subsequent mean, the first is not deleted? - Vlad from Moscow
  • @VladfromMoscow you enter ++++++++, everything should be deleted, + one remains - Rufex

2 answers 2

You also need to escape characters.

^ - matches the beginning of the line
$ - matches the end of the line

similar to other already escaped characters: \/ , \] , \* , \+ , etc.
will look like: \^ , \$

for example from the comment

 ... |\^|\+|\$| ... 

Working example:

 var r = /(\^|\+|\$)/gi; function change(s){ document.getElementById('r').innerHTML = s.replace(r, "_replaced($1)_"); } change('^+$'); 
 <input type="text" onkeyup="change(this.value)" value="^+$"/> <div id="r"></div> 

  • wrote so ... | ^ + $ | ... does not work, can you give an example? - Rufex
  • @Rufex, updated the answer - Grundy

If you carefully write a regular expression pattern, then everything will be deleted as required.

For example,

 <script> window.onload = function () { "USE STRICT"; var pattern = /[абвгдеёжзийклмнопрстуфхцчшщъыьэюя'"\?<>\/\\:;!#%\]\[&\^\*\$\+]/gi; var s = "++++++++"; alert("\"" + s + "\""); s = s.replace(pattern, ""); alert("\"" + s + "\""); }; </script> 

Or, as suggested by @Grundy , a regular expression literal can be written even simpler.

 var pattern = /[А-ЯЁ'"\?<>\/\\:;!#%\]\[&\^\*\$\+]/gi; 
  • then the alphabet can be written at intervals: а-яё - Grundy
  • @Grundy In fact, the alphabet is not ranked sequentially in order of increasing codes. - Vlad from Moscow
  • if I am not mistaken there only the letter ё knocked out. I am wrong? - Grundy
  • @Grundy I don't know. I think that some other characters that fall within this range can be captured. - Vlad from Moscow
  • one
    now checked, from a to я in a row go without spaces, but ё no - Grundy