1

How do I do something like this on a template view?

Example:

{{ field.replace(/ \d*/, "") }}

Where field is a string..

1 Answer 1

1

You can't use regular expression literals in angular expressions (see docs). It means that the closest you can get is creating a regexp object in controller

$scope.regexp = / \d*/;

and then use it in template:

{{ field.replace(regexp, "") }}
Sign up to request clarification or add additional context in comments.

3 Comments

if you really want this logic to be in one place only, you can use <div ng-init="regexp = /blah/">{{field.replace(regexp, '!')}}</div>
@ilj No, you can't. One more time: you can't use regular expression literal in Angular expressions.
you're right. not even with ng-init. but you can have a custom filter that parses RegExp from string parameter and applies it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.