JavaScript - How to Add Spaces Between Words Starting with Capital Letters Using RegExp? Last Updated : 07 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different ways to add spaces between words starting with capital letters using RegExp in JavaScript1. Use Regex with the replace MethodThis is the most direct and widely used approach. JavaScript let s = "HelloWorldThisIsRegex"; s = s.replace(/([A-Z])/g, " $1").trim(); console.log(s); OutputHello World This Is Regex /([A-Z])/: Matches each capital letter individually.(...): Captures the matched capital letter.$1: Refers to the captured group in the replacement string." $1": Adds a space before the matched letter..replace(): Applies the regex replacement to the string..trim(): Removes the leading space caused by adding a space before the first capital letter.2. Split String and RejoinSplit the string at capital letters and rejoin with spaces. JavaScript let s = "HelloWorldThisIsRegex"; let a = s.split(/(?=[A-Z])/).join(" "); console.log(s); OutputHelloWorldThisIsRegex /(?=[A-Z])/: Matches a position (?= is a "lookahead") before any capital letter..split(): Splits the string wherever the regex matches..join(" "): Combines the resulting array into a single string, with spaces between the elements.3. Use a Loop with Regex MatchingIteratively add spaces while traversing the string. JavaScript let s = "HelloWorldThisIsRegex"; let res = ""; for (let n = 0; n < s.length; n++) { res += /[A-Z]/.test(s[n]) && n !== 0 ? " " + s[n] : s[n]; } console.log(res); OutputHello World This Is Regex /[A-Z]/.test(s[n]): Checks if the current character is a capital letter.n !== 0: Ensures no space is added before the first character.result += ...: Builds the resulting string by appending either the character or the character with a space.4. Manually Build the Regex ReplacementIf you want to avoid using the replace function, use a regex match and construct the result. JavaScript let s = "HelloWorldThisIsRegex"; let a = s.match(/[A-Z][a-z]*/g); let res = a.join(" "); console.log(res); OutputHello World This Is Regex /[A-Z][a-z]*/g: Matches a capital letter followed by zero or more lowercase letters, capturing each "word"..match(): Returns an array of all matches..join(" "): Combines the words with spaces. Comment More infoAdvertise with us Next Article JavaScript - How to Add Spaces Between Words Starting with Capital Letters Using RegExp? P parzival_op Follow Improve Article Tags : JavaScript Web Technologies JavaScript-RegExp JavaScript-Questions Similar Reads How to Remove Spaces From a String using JavaScript? These are the following ways to remove space from the given string:1. Using string.split() and array.join() MethodsJavaScript string.split() method is used to split a string into multiple sub-strings and return them in the form of an array. The join() method is used to join an array of strings using 2 min read JavaScript - How to Make First Letter of a String Uppercase? Here are the different approaches to make the first letter of a string uppercase in JavaScript.1. Using charAt() with slice() Method (Most Common)The combination of charAt() and slice() is the simplest and widely used way to capitalize the first letter of a string.JavaScriptconst s1 = "javaScript"; 2 min read Find Word Character in a String with JavaScript RegExp Here are the different ways to find word characters in a string with JavaScript RegExp1. Using \w to Find Word CharactersThe \w metacharacter in regular expressions matches any word character, which includes:A to Z (uppercase letters)a to z (lowercase letters)0 to 9 (digits)Underscore (_)You can use 3 min read JavaScript - How to Replace Multiple Spaces with a Single Space? Here are the various methods to replace multiple spaces with a single space in JavaScript.1. Using replace() Method with Regular Expression (Most Common)The replace() method combined with a regular expression is the most efficient way to replace multiple spaces with a single space.JavaScriptconst s1 2 min read How to convert a string into kebab case using JavaScript ? Given a string with space-separated or camel case or snake case letters, the task is to find the kebab case of the following string. Examples: Input: Geeks For GeeksOutput: geeks-for-geeksInput: GeeksForGeeksOutput: geeks-for-geeksInput: Geeks_for_geeksOutput: geeks-for-geeksBelow are the approaches 3 min read Like