Solved: javascript replace all space

The main problem with replacing all space with a single character is that it can cause problems when text is copied and pasted.

 with underscore

var str = "javascript replace all space with underscore";
str = str.replace(/ /g, "_");

This code line replaces all spaces in the string “javascript replace all space with underscore” with underscores.

Replace in javascript

Replace is a function in JavaScript that takes two arguments. The first argument is the string to be replaced and the second argument is the replacement string.

Working with replace and text

In JavaScript, you can use the replace() and text() functions to replace text in a string.

The replace() function replaces all occurrences of a string in a string with another string. The text() function returns the text of a string, without any formatting.

Related posts:

Leave a Comment