In the first regex, each space character is being replaced, character by character, with the empty string.
In the second regex, each contiguous string of space characters is being replaced with the empty string because of the +
.
However, just like how 0 multiplied by anything else is 0, it seems as if both methods strip spaces in exactly the same way.
If you change the replacement string to '#'
, the difference becomes much clearer:
var str = ' A B C D EF '; console.log(str.replace(/\s/g, '#')); // ##A#B##C###D#EF# console.log(str.replace(/\s+/g, '#')); // #A#B#C#D#EF#
Related Posts:
- Javascript split regex question
- What is a good regular expression to match a URL? [duplicate]
- What is a good regular expression to match a URL? [duplicate]
- RegEx Ignore Case
- JavaScript replace/regex
- Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
- Escaping Discord subset of markdown
- How to test valid UUID/GUID?
- Regular expression : match either of two conditions?
- RegExp in TypeScript
- Converting any string into camel case
- I want to remove double quotes from a String
- Regular expression for removing whitespaces
- Javascript Regular Expression Remove Spaces
- How can I extract a number from a string in JavaScript?
- Remove not alphanumeric characters from string
- Extract hostname name from string
- Difference between ?:, ?! and ?=
- Remove all special characters with RegExp
- How do you use a variable in a regular expression?
- How to count string occurrence in string?
- Regular Expression to reformat a US phone number in Javascript
- Jquery Value match Regex
- RegEx: How can I match all numbers greater than 49?
- Regular expression to validate US phone numbers?
- javascript includes() case insensitive
- RegEx for Javascript to allow only alphanumeric
- Check user logged in with JS
- How to use Redirect in the new react-router-dom of Reactjs
- How to fix “Cannot read property ‘addEventListener’ of null” error [duplicate]
- What does [object Object] mean?
- Remove class using jQuery
- How do I check whether a checkbox is checked in jQuery?
- Babel 7 – ReferenceError: regeneratorRuntime is not defined
- How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
- How can I check for “undefined” in JavaScript?
- How to make history.back() without getting a warning on the browser to reload the page?
- How can I create a two dimensional array in JavaScript?
- Does JavaScript pass by reference?
- How to add jQuery code into HTML Page
- How to loop through an array containing objects and access their properties
- React – TypeError: Cannot read property ‘props’ of undefined
- How to open a local disk file with JavaScript?
- Remove all child elements of a DOM node in JavaScript
- Why Is `Export Default Const` invalid?
- Why is process.env.NODE_ENV undefined?
- Better way to sum a property value in an array
- Javascript require() function giving ReferenceError: require is not defined
- How to remove item from array by value?
- SecurityError: Blocked a frame with origin from accessing a cross-origin frame
- React Error: Target Container is not a DOM Element
- Google Conversions Not Working – Script Won’t Load
- TypeError: Cannot read property ‘get’ of undefined on AngularJS
- How to remove text from a string?
- Convert character to ASCII code in JavaScript
- Disable same origin policy in Chrome
- How do I check whether an array contains a string in TypeScript?
- alert a variable value
- How to check a radio button with jQuery?
- Get current URL with jQuery?
- How to get JSON from URL in JavaScript?
- How do you put an image file in a json object?
- How can I add a class to a DOM element in JavaScript?
- How do I use this JavaScript variable in HTML?
- How to check if a variable is not null?
- How to close current tab in a browser window?
- npm WARN enoent ENOENT: no such file or directory, open ‘C:\Users\Nuwanst\package.json’
- Sorting object property by values
- How to make an HTML back link?
- How to reload/refresh jQuery dataTable?
- How to enable a disabled checkbox dynamically?
- “Cross origin requests are only supported for HTTP.” error when loading a local file
- What is ‘xmlhttp.setRequestHeader();’ and in which situations is it used?
- Gutenberg – remove / add blocks with custom script
- How do I add custom attributes to javascript tags in WordPress?
- Min And Max Date With Date Format In Elementor Form Not Working
- Strange gibberish JavaScript in Editor – site hacked?
- Building a slide down search box in wordpress
- Resolving Javascript errors likely related to Gantry framework
- javascript conflict
- WordPress customizer get control
- How to load JS for specific screen size using wp_enqueue_script?
- I am trying to add current logged in user to my zoho chat
- Video script issue, JavaScript attribute remains ‘undefined’
- Conditional save return on Gutenberg Block
- Headless wp with react. How to handle routes?
- Where to paste Google Map Snippet / JavaScript / CSS for WordPress integration
- Issues trying to get an element by id
- Setting Variable Path to Template Directory inside Script
- Dynamically add Js
- Contact Form 7 Conditional redirect
- How do i load javascript on a specific custom post template via functions.php?
- Dynamically changing logo on scroll function [closed]
- Can’t change javascript files
- Remove WordPress default registration client side validation?
- Live Time on WP showing weirdly [closed]
- Google Map JavaScript error after migrating
- Table block variation with header section activated
- Passing object to FormTokenField suggestions
- How to Update a variable even if the web page reloads in js