How can my regex for finding multiple spaces not match newlines?

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?

--------------Solutions-------------

The issue is that \s represents all whitespace including newlines. If you just want spaces, you can have a literal space:

$('#q').val($('#q').val().replace(/ +/g,' '));

If you want spaces and tabs, you could use a character class instead:

$('#q').val($('#q').val().replace(/[\t ]+/g,' '));

Looking for \x20+ does the trick:

$('#q').val($('#q').val().replace(/\x20+/g,' '));

20 is the Hex code for the space character. You were looking for all whitespace characters, including newlines.

Category:javascript Time:2011-12-13 Views:0

Related post

  • REGEX to replace multiple Spaces with a single space 2009-12-30

    Given a string like: "The dog has a long tail, and it is RED!" What kind of JQUERY, JavaScript magic can be used to keep spaces to only 1 max? Goal: "The dog has a long tail, and it is RED!" thanks --------------Solutions------------- Just replace \s

  • Remove all multiple spaces in Javascript and replace with single space 2010-07-20

    This question already has an answer here: REGEX to replace multiple Spaces with a single space 13 answers How can I automatically replace all multiple spaces with one single space in Javascript? I've tried with just chaining some s.replace but this d

  • RegEx how to replace multiple spaces with a single one 2011-08-08

    Possible Duplicate: Remove all multiple spaces in Javascript and replace with single space How can I replace, for instance this: " " " " " " " " with " " How can I replace all the empty space characters with a single empty char using regex? ---------

  • Regex - Replacing multiple spaces with tab 2011-12-10

    I'm using the code sample below, and what I want to do is replace all multiple spaces with 1 \t. So "I am having fun working on Regex" returns "I am\thaving fun\tworking\ton\tRegex" Current code: RegexOptions options = RegexOptions.None; Regex regex

  • Regex or way to replace multiple space with single space 2013-09-16

    Could you please let me know is there any way to replace multiple space with single space in java or spring? Any stringUtils function for same? like 1. test test test test 2. test test test test 3. test test test test --------------Solutions---------

  • How do I replace multiple spaces with a single space in C#? 2008-10-15

    How can I replace multiple spaces in a string with only one space in C#? Example: 1 2 3 4 5 would be: 1 2 3 4 5 --------------Solutions------------- RegexOptions options = RegexOptions.None; Regex regex = new Regex(@"[ ]{2,}", options); tempo = regex

  • A simple way to remove multiple spaces in a string in Python 2009-10-09

    Suppose this is the string: The fox jumped over the log. It would result in: The fox jumped over the log. What is the simplest, 1-2 liner that can do this? Without splitting and going into lists... --------------Solutions------------- >>> im

  • Linq: Search a string for all occurrences of multiple spaces 2010-04-29

    I have a string and i want to find the position of all the occurrences of multiple spaces. Im writing a punctuation checker. I would like to parralelise this operation using parallel linq but in the meantime im just looking for the linq method to get

  • Regular Expression in Visual Studio Find & Replace - multiple spaces between search terms 2010-08-23

    I require a regular expression for the Visual Studio Search and Replace functionality, as follows: Search for the following term: sectorkey in ( There could be multiple spaces between each of the above 3 search terms, or even multiple line breaks/car

  • Replace multiple spaces with one space in a string 2011-12-02

    How would I do something like this (java) string.replaceAll(" ", " ") in c++ this would make it so that all multiple spaces become just one space. --------------Solutions------------- bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) &

  • disabling multiple space in text area javascript 2012-04-12

    To avoid typing multiple spaces in text area I'm calling a function on onCLick, on keyup and written the code below var val = document.getelemntByID('trmp'); val.value = val .value.replace(/ +(?= )/g,''); this is working fine in Firefox, in IE7 it's

  • How to remove multiple spaces and new lines from a string in PHP? 2012-04-24

    I have a form with a text area, I need to remove from the string entered here eventuals multiple spaces and multiple new lines. I have written this function to remove the multiple spaces function fix_multi_spaces($string) { $reg_exp = '/\s+/'; return

  • Using regex to replace all spaces NOT in quotes in Ruby 2008-10-15

    I'm trying to write a regex to replace all spaces that are not included in quotes so something like this: a = 4, b = 2, c = "space here" would return this: a=4,b=2,c="space here" I spent some time searching this site and I found a similar q/a ( Split

  • How do I replace multiple spaces with a single space? 2009-08-01

    Well I'm looking for a function that reduce multiple space characters ' ' in a string. For example for string s given : s="hello__________world____!" The function must return "hello_world_!" In python we can do it via regexp simply as: re.sub("\s+",

  • Regex Question: how do I replace a single space with a newline in VI 2010-02-05

    Regex Question: how do I replace a single space with a newline in VI. --------------Solutions------------- :%s/ /^V^M/g note: hit ctrl-v, ctrl-m. edit: if you really mean all single spaces, meaning spaces not followed by another space, use this: :%s/

  • php Replacing multiple spaces with a single space 2010-03-03

    I'm trying to replace multiple spaces with a single space. When I use ereg_replace, I get an error about it being deprecated. ereg_replace("[ \t\n\r]+", " ", $string); Is there an identical replacement for it. I need to replace multiple " " white spa

  • sscanf with multiple spaces? 2010-05-24

    sscanf(text, "%s %s", name, company); parses 'ian mceknis' but it also parses 'ian mceknis' and so on. How can i make this to parse only the first one? It must contain only one space not more. Thank you. --------------Solutions------------- If you wa

  • Multiple Spaces Between Words in HTML without   2010-12-21

    I'm using a barcode font "Free 3 of 9 Extended Regular" and the print out needs to have multiple spaces in some cases, to match the number of characters in the field. Example: *AA-XXXX *" (four spaces follow the item number to make the 12 characters.

  • PHP Insert Multiple Spaces 2011-04-01

    I've got some data that needs to be cleaned up into a fixed length format. I'm using PHP to grab the data out, covert it, and put it back in, but it's not working as planned. There is a certain point in each piece in the middle of the data where ther

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.523 (s). 13 q(s)