角过滤器全部更换彰显了空间 [英] Angular filter to replace all underscores to spaces

查看:153
本文介绍了角过滤器全部更换彰显了空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个过滤器,以取代所有的下划线为空格在一个字符串,所以我建立了这个:

I need a filter to replace all the underscores to spaces in a string so I built this:

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

这是在一个字符串的第一个下划线做工精细,而不是是否有更多的。

It is working fine on the first underscore in a string, not if there are more.

hello_worlds          => hello world
hello_beautiful_world => hello beautiful_world

我该如何解决呢?

How can I fix that?

推荐答案

与string.replace 不是只接受字符串作为第一个参数,而且它接受正则表达式作为第一个参数。所以把 _ 正则表达式定界符中 / 和ASLO添加先按g 与一起修改。 先按g 称为全局修饰符,将在全球范围做了更换。

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.

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

这篇关于角过滤器全部更换彰显了空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆