Angular filter to replace all underscores to spaces
Asked Answered
S

6

26

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

Sagunto answered 11/8, 2015 at 10:27 Comment(0)
P
44

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, ' ');
  };
});
Philana answered 11/8, 2015 at 10:28 Comment(2)
This fail if input isn't a string, or if input is null or undefined.Pewter
simple, just use an if stmt for condition checking.Philana
I
23

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:

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

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

Incuse answered 20/1, 2017 at 23:58 Comment(2)
Where shud I add App.filter code ? and Which version of angular is this..Fend
This is for AngularJS 1.x. App here represents the object you get back from creating your Angular app as follows: var App = angular.module('myApp', ['some-dependency', 'some-other-dependency']);Incuse
N
7

In some case, you can use split() function.
.replace function is not compliant with regexp syntax (i.e. .replace(/,/g,'\n') syntax)

Full syntax:
{{myVar.toString().split(',').join('\n')}}

.toString() function is in case myVar is not typed as String in typescript.

Novelia answered 20/6, 2019 at 15:36 Comment(1)
This is the perfect answer, simple and optimized.Smaragdite
R
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.

Radiotelegraphy answered 18/4, 2017 at 13:47 Comment(3)
BratisLatas I liked your method, however it appears to only replace only one instance, not multiple.Eunuchize
I don't think that's a valid angular expression. It does work if the first argument is a string, but if using the regular expression, I get syntax Error: Token '/' not a primary expression.Placenta
Works well for single character replacementIva
D
1

This simple function can do it:

public getCleanedString(cadena) {
    cadena = cadena.replace(/_/g, ' ');
    return cadena;
  }
Deweydewhirst answered 13/9, 2018 at 7:54 Comment(1)
What question are you answering? They want to replace underscore with space.Dewain
G
0

this is i used in angularjs 1.4.7

<li ng-show="filter.degree.length"> 
    <label>Search by Degree :- </label> {{
        filter.degree.toString().split('|').join(', ')
    }} 
</li>
Gamaliel answered 27/2, 2020 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.