Regex jquery removes all double spaces

Hi, I have this code, I want it to remove all double spaces from the text area, but every time it only deletes the first event.

$(document).ready(function(){
  $("#article").blur(function(){
    ///alert($(this).val());
    $(this).val($(this).val().replace(/\s\s+/, ' '));
  });
});

I also tried removeAll (), but it does not work at all. thanks. I have a live example online at http://jsbin.com/ogasu/2/edit

+3
source share
2 answers

Use the g modifier in your regular expression to match and replace globally:

/\s\s+/g

Otherwise, only the first match will be replaced.

, jQuery 1.4 val , :

$(this).val(function(index, value) {
    return value.replace(/\s\s+/g, ' ');
});

$(this).val.

+8
.replace(/\s\s+/g, ' '));

g

+3

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


All Articles