Given that you also want to cover tabs, newlines, etc, just replace \s\s+
with ' '
:
string = string.replace(/\s\s+/g, ' ');
If you really want to cover only spaces (and thus not tabs, newlines, etc), do so:
string = string.replace(/ +/g, ' ');
More Related Contents:
- Not able to get Regular Expression in javascript
- Need Pattern for password field?
- Regular Expression jquery validation1 [duplicate]
- Regular expression to match non-ASCII characters?
- jQuery selector regular expressions
- Including a hyphen in a regex character bracket?
- Extract hostname name from string
- split string only on first instance of specified character
- Remove whitespace and line breaks between HTML elements using jQuery
- How to pull the file name from a url using javascript/jquery?
- Get URL from background-image Property
- Wrap tags around http text
- javascript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase [duplicate]
- Extract all email addresses from bulk text using jquery
- Split a string based on multiple delimiters
- Insert dash after every 4th character in input
- Insert space before capital letters
- Highlight current page in jquery
- jQuery Youtube URL Validation with regex
- How to convert a Title to a URL slug in jQuery?
- Regular expression with the cyrillic alphabet
- Regex in JavaScript for validating decimal numbers
- Regular expression for only characters a-z, A-Z
- regular expression to validate datetime format (MM/DD/YYYY) [duplicate]
- Regex for allowing alphanumeric,-,_ and space
- Why this regex is not working for german words?
- Remove everything within script and style tags
- Match all URLs in string and return in array in JavaScript
- Regular Expression to MATCH ALL words in a query, in any order
- Find and replace nth occurrence of [bracketed] expression in string