HOWTO · JavaScript

How to Replace Space With Dash in JavaScript

Learn how to replace literal spaces with dashes in JavaScript using replaceAll() and replace().

On this page

JavaScript strings are immutable: replaceAll() and replace() return a new string and leave the original unchanged. Use replaceAll(' ', '-') to change every ordinary space to a dash. Use replace(' ', '-') when only the first ordinary space should change. The examples below use console.log() to make each result visible when you run them in a JavaScript environment.

Replace Literal Spaces with replaceAll()

A string search pattern is matched literally, so ' ' means one ordinary space; it is not a regular expression. This is the direct choice when every ordinary space should become a dash. The original string remains available if you need it again.

Run this example:

const phrase = 'Hello World! Welcome to my blog post.';
const dashed = phrase.replaceAll(' ', '-');
console.log(dashed);
Hello-World!-Welcome-to-my-blog-post.

For broader whitespace, use a global regular expression such as /\s/g. The \s pattern matches whitespace characters, including tabs and line breaks, as well as ordinary spaces. replaceAll() requires the g flag when its search pattern is a RegExp; without it, the call throws a TypeError. A string pattern has no flags and is treated literally.

const text = 'Hello\tWorld! Welcome\nto my blog post.';
const dashedWhitespace = text.replaceAll(/\s/g, '-');
console.log(dashedWhitespace);
Hello-World!-Welcome-to-my-blog-post.

The replacement can also be a function when the new text depends on each match. For the simple space-to-dash task, a string replacement is clearer. Both forms produce a new string rather than modifying the source value.

For more details, see the MDN documentation for replaceAll().

Replace Literal Spaces with replace()

When the search pattern is a string, replace() changes only its first occurrence. That makes it useful when you want to change the first separator and preserve later spaces.

Run this example:

const phrase = 'Hello World! Welcome to my blog post.';
const firstOnly = phrase.replace(' ', '-');
console.log(firstOnly);
Hello-World! Welcome to my blog post.

To replace every ordinary space with replace(), use a global regular expression: / /g. The pattern contains a literal space, and the g flag tells replace() to continue through all matches. A RegExp without g is also valid with replace(); it replaces the first match rather than throwing a TypeError.

const phrase = 'Hello World! Welcome to my blog post.';
const everySpace = phrase.replace(/ /g, '-');
console.log(everySpace);
Hello-World!-Welcome-to-my-blog-post.

As with replaceAll(), the pattern /\s/g can match broader whitespace instead of only ordinary spaces. Choose the pattern based on the characters in the input that should change. For the replace() method’s search and replacement behavior, see the MDN documentation.

Choose Between replace() and replaceAll()

Choose replaceAll(' ', '-') when you want every literal ordinary space changed and prefer a string pattern. Choose replace(' ', '-') when you want only the first literal ordinary space changed. If using replace() to change all ordinary spaces, / /g provides that behavior. If the task includes tabs or line breaks, use /\s/g with either method.

The RegExp flag rule differs: replaceAll() requires a global RegExp, while replace() accepts either a global or non-global RegExp. A global RegExp replaces every match with either method; a non-global one replaces only the first. The distinction between a string pattern and a RegExp also matters: ' ' matches an ordinary space literally, while /\s/g matches whitespace more broadly. Select the method and pattern that match both the intended number of replacements and the intended characters.