Reputation: 2365
Right now my code is able to remove additional spaces, but it seems to mess up with new lines. If there are multiple breaks in the input, it removes all lines breaks. For example:
Input:
abc xyz
test
Output:
abc xzy test
Ideally, I'd like the output to be:
abc xyz
test
Code:
var input = $('textarea#input');
var output = $('textarea#output');
input.bind('keyup', function() {
output.val(input.val().replace(/(\s){2,}/g, ' '));
});
jsFiddle http://jsfiddle.net/rdvR7/3/
I would also like to keep tabs, but haven't figured out how to use \t
.
Upvotes: 3
Views: 1050
Reputation: 113896
Just replace the space character (which represents itself in regex):
output.val(input.val().replace(/ {2,}/g, ' '));
Upvotes: 0
Reputation: 6582
You could use character classs to replace only spaces.
output.val(input.val().replace(/[ ]{2,}/g,' '));
This would only find spaces, not tab characters and newlines. You could also put any combination of characters inside the brackets
output.val(input.val().replace(/[ \t]{2,}/g,' '));
This would find all spaces and all tab characters.
Upvotes: 4