Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  166] [ 5]  / answers: 1 / hits: 197080  / 10 Years ago, fri, august 15, 2014, 12:00:00

With c# there is a string.Replace-method.
Like This:



string oldString = stackoverflow;   

string newString= oldString.Replace(stackover,);


Output: flow



Can I do something similar to this with AngularJs?



My try doesn't work:



var oldString = stackoverflow;

$scope.newString= oldString.Replace(stackover,NO);

More From » angularjs

 Answers
5

In Javascript method names are camel case, so it's replace, not Replace:



$scope.newString = oldString.replace(stackover,NO);


Note that contrary to how the .NET Replace method works, the Javascript replace method replaces only the first occurrence if you are using a string as first parameter. If you want to replace all occurrences you need to use a regular expression so that you can specify the global (g) flag:



$scope.newString = oldString.replace(/stackover/g,NO);


See this example.


[#69767] Wednesday, August 13, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
noel

Total Points: 49
Total Questions: 90
Total Answers: 104

Location: Aland Islands
Member since Wed, Nov 17, 2021
3 Years ago
noel questions
;