Given a string and the task is to replace all lowercase letters from a string with the other characters in JavaScript. There are some approaches to replace the character that are described 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 a required parameter. It specifies the value or regular expression, that is going to replace by the new value.
- newvalue: It is a required parameter. It specifies the value to be replaced by the search value.
Return value: It returns a new string that matches the pattern specified in the parameters.
Example 1: This example replaces the lowercase letters with . (dot) using replace() method.Â
Javascript
var str = "WELCOMEiTOgGEEKS" ; Â
console.log(str.replace(/[a-z]/g, "." )); |
WELCOME.TO.GEEKS
Example 2: This example replaces the lowercase letters with (‘ ‘)(space) by going to the each character and checking if it is lowercase.Â
Javascript
var str = "THISiISgGEEKSFORGEEKS!" ; Â
function gfg_Run() { Â Â Â Â let newStr = "" ; Â
    for (let i = 0; i < str.length; i++) {         if (str.charCodeAt(i) >= 97 &&             str.charCodeAt(i) <= 122) {             newStr += ' ' ;         }         else {             newStr += str[i];         }     }     console.log(newStr); } Â
gfg_Run(); |
THIS IS GEEKSFORGEEKS!
Example 3: In this example, we will use reduce method to replace all the lowerCase characters from string.
Javascript
let str = "THISiISgGEEKSFORGEEKS!" ; Â
function check(x) { Â Â Â Â if (x >= 'a' && x <= "z" ) return false ; Â
    return true ; } Â
function gfg_Run() { Â Â Â Â let newStr = [...str].reduce((accu, x) => Â Â Â Â Â Â Â Â check(x) ? accu + x : accu + "." , '' ); Â
    console.log(newStr); }   Â
gfg_Run(); |
THIS.IS.GEEKSFORGEEKS!