I run this regex on #q keyup event to avoid extra spaces in a string.
$('#q').val($('#q').val().replace(/\s+/g,' '));
The problem is that it is also deleting all new lines. How can I delete extra spaces but keep new lines intact?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The issue is that
\srepresents all whitespace including newlines. If you just want spaces, you can have a literal space:If you want spaces and tabs, you could use a character class instead: