Replacing spaces with underscores in JavaScript Last Updated : 20 Jun, 2023 Comments Improve Suggest changes Like Article Like Report Given a sentence and the task is to replace the spaces(" ") from the sentence with underscores("_") in JavaScript. There are some JavaScript methods are used to replace spaces with underscores which are listed below: JavaScript replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue) Parameters: searchVal: It is required parameter. It specifies the value, or regular expression, that is going to replace by the new value.newvalue: It is required parameter. It specifies the value to replace the search value with. JavaScript split() method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split(separator, limit) Parameters: separator: It is optional parameter. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item).limit: It is optional parameter. It holds the integer that specifies the number of splits, items beyond the split limit will be excluded from the array. Example 1: This example replaces all spaces(' ') with underscores("_") by using replace() method. JavaScript let str = "A Computer Science portal for Geeks."; console.log(str); console.log(str.replace(/ /g, "_")); OutputA Computer Science portal for Geeks. A_Computer_Science_portal_for_Geeks. Example 2: This example replaces all spaces(' ') with underscores("_") by using split() method. It first splits the string with spaces(" ") and then join it with underscore("_"). JavaScript let str = "A Computer Science portal for Geeks."; console.log(str); console.log(str.split(' ').join('_')); OutputA Computer Science portal for Geeks. A_Computer_Science_portal_for_Geeks. Comment More infoAdvertise with us Next Article Replacing spaces with underscores in JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Replace special characters in a string with underscore (_) in JavaScript We will explore several methods to replace special characters in a string with an underscore (_) in JavaScript. Special characters are non-alphanumeric symbols, and replacing them is a common requirement in various use cases such as sanitizing input or formatting file names.Table of ContentJavaScrip 2 min read What does +_ operator mean in JavaScript ? Unary Operator: A unary operation contain only one operand. Here, the '+' unary plus operator converts its operand to Number type. While it also acts as an arithmetic operator with two operands which returns an addition result on calculation. JavaScript Identifiers: Javascript Identifiers are used t 2 min read Underscore.js _.without() Function Underscore.js _.without() function is used to return a copy of the array which contains all the array except values. Syntax:_.without( array, *values );Parameters:array: This parameter is used to hold the list of array elements.values: This parameter is used to hold the value that needs to be remove 3 min read Underscore.js _.isString() Function The _.isString() function is used to check whether the given object element is string or not. Syntax: _.isString( object ) Parameters: This function accepts single parameter as mentioned above and described below: object: It contains the value of object that need to be check whether it is an string 1 min read Underscore.js _.pairs() Function Underscore.js _.pairs() function is used to convert an object into an array of arrays that contain the [key, value] pairs of the object as elements. Syntax:_.pairs( object );Parameters:object: It contains the object element that holds the elements of key and value pair.Return Value:It returns the ar 1 min read Underscore.js _.template() Function Underscore.js _.template() function is used to compile JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources. Template functions to create a template function that is compiled and can interpolate properties of da 2 min read Underscore.js _.isMatch() Function It _.isMatch() function: is used to find out if the property given in argument is present in the passed array or not. Also, the property's value should be the same in order to match. It is used in cases where we want to find whether the array satisfies a specific condition or not. Syntax:_.isMatch(o 3 min read Underscore.js Introduction Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day to day common programming tasks. With just under six kilobytes in size, this library basically provides us with a w 2 min read Underscore.js _.matcher() Function Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc. even without using any built-in objects. The _.matcher() function is an inbuilt function in Underscore.js library of JavaScript which is used to 1 min read Like