角度过滤器将所有下划线替换为空格 [英] Angular filter to replace all underscores to spaces

查看:30
本文介绍了角度过滤器将所有下划线替换为空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

string.replace 不仅接受字符串作为第一个参数,还接受正则表达式作为第一个参数.因此,将 _ 放在正则表达式分隔符 / 中,并添加 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天全站免登陆