ulou
ulou

Reputation: 5863

Angular2 pipe regex

I did research, but I didn't find good article about angular2 regex inside pipe. I would like to create custom pipe that will trim displaying word.

I have:

aaaa.bbbb.cccc.dddd(yyyy,pppp.yy)

I want display only:

cccc.dddd

There is always '(' in this word. I would like to use RegExp here or maybe it is better way to do this? Question is about how to use RegExp in Pipe, not about RegExp pattern.

UPDATE

working example (last version)

Upvotes: 0

Views: 918

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627335

You do not really need to use any regex here, you can split with ( and get the first item:

In your CustomPipe class, use

return value.indexOf('(') > -1 ? value.split('(')[0] : value;

See the updated plunkr.

Another option is to use value.substring(0,value.indexOf("(")) instead of value.split('(')[0].

Upvotes: 2

Related Questions