简体   繁体   English

在AngularJs中的string.Replace

[英]string.Replace in AngularJs

With c# there is a string.Replace-method. 使用c#,有一个string.Replace-method。 Like This: 像这样:

string oldString = "stackoverflow";   

string newString= oldString.Replace("stackover","");

Output: flow 输出: flow

Can I do something similar to this with AngularJs? 我可以用AngularJs做类似的事吗?

My try doesn't work: 我的尝试不起作用:

var oldString = "stackoverflow";

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

In Javascript method names are camel case, so it's replace , not Replace : 在Javascript方法名称是camel case,所以它是replace ,而不是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. 请注意,与.NET Replace方法的工作原理相反,如果您使用字符串作为第一个参数,则Javascript replace方法仅替换第一个匹配项。 If you want to replace all occurrences you need to use a regular expression so that you can specify the global (g) flag: 如果要替换所有实例,则需要使用正则表达式,以便指定global(g)标志:

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

See this example. 这个例子。

The easiest way is: 最简单的方法是:

var oldstr="Angular isn't easy";
var newstr=oldstr.toString().replace("isn't","is");
var oldString = "stackoverflow";
var str=oldString.replace(/stackover/g,"NO");
$scope.newString= str;

It works for me. 这个对我有用。 Use an intermediate variable. 使用中间变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM