1

I have some text I am trying to replace a part of with regex in Angularjs.

The text will be in the format:

Name: Jane
Age: 66
Address: 31 Test Street

I would like to replace the part after Age: with 'REDACTED' (so 66 will not appear), so I have created this filter which to me seems like it should work:

angular.module('person')
    .controller(controllerId, personControl)
.filter('ageFilter', function () {
    return function (text) {
        if (text)
            return text.replace(/[\n\r].*Age: \s*([^\n\r]*)/, 'REDACTED');
        return '';
    }
});

The relevant part of the html:

<dd ng-bind-html="vm.model.PERSON_INFO | ageFilter"></dd>

The filter is hit but the text is never replaced. My regex works when I test on Regex test websites, so I think I may be doing something else wrong which I cannot see.

4
  • 1
    Create a plunker demo that reproduces issue Commented Nov 24, 2017 at 5:31
  • Just a pointer, should you not assign the return value so that it is reflected in view? Commented Nov 24, 2017 at 5:38
  • I think that problem is with your text, will it have \n in it? Commented Nov 24, 2017 at 5:49
  • Shouldn't text.replace(/^(Age:[\t ]*).*/m, '$1REDACTED'); suffice? See this regex demo. BTW, your regex does not work well. Commented Nov 24, 2017 at 7:44

1 Answer 1

1

Try this:

return text.replace(/([\n\r]+.*Age: )(\s*)([^\n\r]*)/, '$1REDACTED$2')
Sign up to request clarification or add additional context in comments.

Comments

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.