How to replace lowercase letters with an other character in JavaScript ?
Given a string and the task is to replace all lowercase letters from a string with the other characters. The method which are used to replace the character are described below:
-
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 be replaced the search value.
Return value: It returns a new string where the defines value has been replaced by the new value.
Example 1: This example replaces the lowercase letters with . (dot) using replace() method.
<!DOCTYPE HTML> <html> <head> <title> Replace lowercase letters with an other characters </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 16px; font-weight: bold;"> </p> <button onclick = "gfg_Run()"> Click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = "THISiISgGEEKSFORGEEKS!"; el_up.innerHTML = str; function gfg_Run() { el_down.innerHTML = str.replace(/[a-z]/g, "."); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example replaces the lowercase letters with (‘ ‘)(space) by going to the each character and checking if it is lowercase, If it is then replacing occurs.
<!DOCTYPE HTML> <html> <head> <title> Replace lowercase letters with an other character </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 16px; font-weight: bold;"> </p> <button onclick = "gfg_Run()"> Click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = "THISiISgGEEKSFORGEEKS!"; el_up.innerHTML = str; function gfg_Run() { var newStr = ""; for(var i=0; i<str.length; i++) { if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) { newStr += ' '; } else { newStr += str[i]; } } el_down.innerHTML = newStr; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to replace a character at a particular index in JavaScript ?
- JavaScript methods to increment/decrement alphabetical letters
- JavaScript | string.replace()
- JavaScript | Replace() Method
- How to replace an item from an array in JavaScript?
- How to replace all dots in a string using JavaScript?
- How to replace plain URL with link using JavaScript ?
- Replace special characters in a string with underscore (_) in JavaScript
- How to globally replace a forward slash in a JavaScript string ?
- How to get the last character of a string in JavaScript?
- How to find out which character key is pressed using JavaScript?
- How to remove a character from string in JavaScript ?
- How to get character array from string in JavaScript?
- Delete first character of a string in JavaScript
- How to remove portion of a string after certain character in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


