How to replace a string by another string in AngularJS ? Last Updated : 01 Aug, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to select a string from the text and replace it with another string using AngularJS and will understand it through the illustrations. The replace() method can be used to search the particular character(s), which is getting replaced with a specific character(s) & return a new generated string. Syntax: string.replace(char findChar, char replacedChar);Parameter Values: findChar: It represents the character that is to be searched & replaced with the new character.replacedChar: It represents the new replaced character, by replacing the findChar, that generates the new string.Return Value: It returns the new string containing the new character(s) that have replaced the specified character(s). Approach: The approach is utilizing the replace() method and replacing the content of the string with the new one. In the first example, the string 'Welcome User' is replaced by the word 'Geek' in place of 'User'. In the second example, the word 'User' is replaced by the string user enters in the input element. Example 1: In this example, the word "User" is getting replaced with the new word "Geek" using the replace() method. HTML <!DOCTYPE HTML> <html> <head> <script src= "//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"> </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function($scope) { $scope.str1 = "Welcome User"; $scope.str2 = ""; $scope.remSpaces = function() { $scope.str2 = $scope.str1.replace("User", "Geek"); }; }); </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Replace a string by another in AngularJS </h3> <div ng-app="app"> <div ng-controller="controller"> String = '{{str1}}' <br><br> <button ng-click='remSpaces()'> Replace User </button> <br><br> Replaced String = '{{str2}}' <br> </div> </div> </body> </html> Output: Example 2: In this example, the input character is getting replaced with the searched character with the help of replace() method. HTML <!DOCTYPE HTML> <html> <head> <script src= "//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"> </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function($scope) { $scope.str1 = "Welcome User"; $scope.str2 = ''; $scope.replaceStr = function() { // Get & replace the input value $scope.str1 = $scope.str1.replace("User", $scope.str2); }; }); </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Replace a string by another in AngularJS </h3> <div ng-app="app"> <div ng-controller="controller"> Enter here: <input type="text" name="strExample" ng-model="str2"> <br><br> <button ng-click='replaceStr()'> Replace </button> <br><br> String = {{str1}}<br> </div> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article How to replace a string by another string in AngularJS ? P PranchalKatiyar Follow Improve Article Tags : Web Technologies HTML AngularJS HTML-Misc AngularJS-Misc +1 More Similar Reads How to replace a text in a string with another text using PHP ? A string is a sequence of one or more characters. A string is composed of characters, each of which can be replaced easily in the script. A large number of in-built PHP methods can be used to perform string replacements. Table of ContentUsing str_replace() method - The str_replace() Using str_irepla 4 min read How to convert string into a number using AngularJS ? In this article, we will see how to convert a string into a number in AngularJS, along with understanding its implementation through the illustrations. Approach: The parseInt() method is used for converting the string to an integer. We will check whether the string is an integer or not by the isNumb 2 min read Format a Date using ng-model in AngularJS The ng-model directive is used to bind the value of the input field to a property made in the controller. Formatters are used to convert the value of input from one textural representation to another desired representation. Formatters used for date formatting are useful when the date needs to be upd 3 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read How to use javascript function in string interpolation in AngularJS ? String interpolation is basically used to display the dynamic data on an HTML template. It facilitates you to make changes on component.ts file and automatically fetch data from there to the HTML template (component.html file). So here we create a simple HTML template that uses the string interpolat 2 min read How to concat strings using AngularJS ? In this article, we will see how to concat strings in AngularJS. There are few ways to concat the strings in AngularJS. In this article, we will see 2 of them.Example 1: In the first example, we are using the '+' operator to concat the strings HTML<!DOCTYPE HTML> <html> <head> < 2 min read How to Change Image Source URL using AngularJS ? Here the task is to change the source URL of the image with the help of AngularJS.Approach: The approach is to change the URL of the image when the user click on button. When a user clicks on a button then a method is called along with the new URL, that method replaces the new URL with the old one i 2 min read JavaScript - How to Globally Replace a Forward Slash in a String? Here are the various methods to globally replace a forward slash in the JavaScript string.1. Using replace() Method with a Regular ExpressionThe replace() method is commonly used with a regular expression to replace all occurrences of a forward slash.JavaScriptconst s1 = "path/to/some/resource"; con 2 min read How to replace multiple characters in a string in PHP ? A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla 3 min read How to Sort an Array based on User Input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. In this article, we will see how to sort an array based o 3 min read Like