角过滤器,以取代所有下划线的空间

时间:2023-02-03 19:30:36

I need a filter to replace all the underscores to spaces in a string

我需要一个过滤器将所有下划线替换为字符串中的空格

3 个解决方案

#1


31  

string.replace not only accepts string as first argument but also it accepts regex as first argument. So put _ within regex delimiters / and aslo add g modifier along with that. g called global modifier which will do the replacement globally.

字符串。replace不仅接受字符串作为第一个参数,还接受regex作为第一个参数。所以在regex分隔符/中放入_,并在其中添加g修饰符。g称为全局修改器,它将对全局进行替换。

App.filter('underscoreless', function () {
  return function (input) {
      return input.replace(/_/g, ' ');
  };
});

#2


11  

Here's a generic replace filter alternative

这是一个通用的替换过滤器

App.filter('strReplace', function () {
  return function (input, from, to) {
    input = input || '';
    from = from || '';
    to = to || '';
    return input.replace(new RegExp(from, 'g'), to);
  };
});

Use it as follows in your HTML:

在HTML中使用如下:

{{ addText | strReplace:'_':' ' }}

Minor note: Any HTML tags in the to parameter will cause the expression to fail due to Angular content security rules.

次要注意:to参数中的任何HTML标记都将导致表达式由于有角的内容安全规则而失败。

#3


1  

There is a easyer method:

有一个简易方法:

You could replace it inline without a defined filter. This is the way.

您可以在没有定义过滤器的情况下内联地替换它。就是这样。

This example its for replace just in the view.

在这个例子中,它表示在视图中进行替换。

{{ value.replace(/_/g, ' ') }}

I hope its could help in a simple change, if you want to change in more places, use the filter.

我希望它可以帮助一个简单的改变,如果你想改变更多的地方,使用过滤器。

#1


31  

string.replace not only accepts string as first argument but also it accepts regex as first argument. So put _ within regex delimiters / and aslo add g modifier along with that. g called global modifier which will do the replacement globally.

字符串。replace不仅接受字符串作为第一个参数,还接受regex作为第一个参数。所以在regex分隔符/中放入_,并在其中添加g修饰符。g称为全局修改器,它将对全局进行替换。

App.filter('underscoreless', function () {
  return function (input) {
      return input.replace(/_/g, ' ');
  };
});

#2


11  

Here's a generic replace filter alternative

这是一个通用的替换过滤器

App.filter('strReplace', function () {
  return function (input, from, to) {
    input = input || '';
    from = from || '';
    to = to || '';
    return input.replace(new RegExp(from, 'g'), to);
  };
});

Use it as follows in your HTML:

在HTML中使用如下:

{{ addText | strReplace:'_':' ' }}

Minor note: Any HTML tags in the to parameter will cause the expression to fail due to Angular content security rules.

次要注意:to参数中的任何HTML标记都将导致表达式由于有角的内容安全规则而失败。

#3


1  

There is a easyer method:

有一个简易方法:

You could replace it inline without a defined filter. This is the way.

您可以在没有定义过滤器的情况下内联地替换它。就是这样。

This example its for replace just in the view.

在这个例子中,它表示在视图中进行替换。

{{ value.replace(/_/g, ' ') }}

I hope its could help in a simple change, if you want to change in more places, use the filter.

我希望它可以帮助一个简单的改变,如果你想改变更多的地方,使用过滤器。