Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegExp literals in expressions support #21978

Closed
sam-s4s opened this issue Feb 1, 2018 · 6 comments
Closed

RegExp literals in expressions support #21978

sam-s4s opened this issue Feb 1, 2018 · 6 comments
Labels
area: core Issues related to the framework runtime core: binding & interpolation Issue related to property/attribute binding or text interpolation feature Issue that requests a new feature
Milestone

Comments

@sam-s4s
Copy link

sam-s4s commented Feb 1, 2018

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ * ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When putting a RegExp inside a template, the compiler fails.

Expected behavior

It should compile the template correctly, and use the RegExp normally.

Minimal reproduction of the problem with instructions

<div *ngFor="let a of b | blah : /\d+/"></div>

What is the motivation / use case for changing the behavior?

It would be much more convenient to use RegExps directly. Otherwise you have to...
a) pass a string
b) create a RegExp using that string on the other end (in the case of a pipe)
c) use double backslashes to escape things like \d, which breaks actual RegExp rules, meaning you need to modify your RegExp when creating and testing, etc.
d) remember to remove the leading and trailing slashes, as the string input for RegExp doesn't like those

Environment


Angular version: 4.4.4
@sam-s4s sam-s4s changed the title RegExp in templates RegExp in templates for pipes Feb 1, 2018
@trotyl
Copy link
Contributor

trotyl commented Feb 2, 2018

Duplicate of #6419

@gkalpak gkalpak added feature Issue that requests a new feature area: core Issues related to the framework runtime labels Feb 2, 2018
@ngbot ngbot bot added this to the Backlog milestone Feb 2, 2018
@gkalpak
Copy link
Member

gkalpak commented Feb 2, 2018

@sam-s4s, RegExp literals are not supported indeed, but you can still create a RegExp in your component and pass that:

@Component({
  ...
  template: '{{ '123abc' | myPipe:re }}
}
class MyComponent {
  re = /\d+/;
}

Although is would indeed be cool to have RegExp literal support, note that the templating language is not supposed to have feature-parity with JavaScript (it is just a subset with some extra Angular-specific semantics). Given that and the fact that parsing RegExps would be quite complicated (esp. if you also had to validate them 😃), my guess would be that RegExp literals will remain unsupported.

@trotyl, thx for pointing out that other issue. Since it didn't have an explanation of why RegExp literals are not supported in templates, I'll leave this one open as a feature request.

Let's see what the compiler people think.

trotyl added a commit to trotyl/angular that referenced this issue Feb 19, 2018
trotyl added a commit to trotyl/angular that referenced this issue Feb 19, 2018
trotyl added a commit to trotyl/angular that referenced this issue Feb 20, 2018
trotyl added a commit to trotyl/angular that referenced this issue Feb 20, 2018
@trotyl
Copy link
Contributor

trotyl commented Feb 20, 2018

Let's see what the compiler people think.

I can act as compiler people as long as they're willing to have it. 😃

trotyl added a commit to trotyl/angular that referenced this issue Feb 20, 2018
trotyl added a commit to trotyl/angular that referenced this issue Feb 20, 2018
@sam-s4s
Copy link
Author

sam-s4s commented Feb 20, 2018

I think it'd be handy :)

In this particular use-case it's for a pipe, and one of the handy things about pipes is that they can be re-used in multiple locations in the template. That'd be kinda ruined if you then had to make all these variables to store your regexp in :P

I know it's not a huge issue, but maybe it'd be nice.

I don't think parsing the regexp is important - only finding the start and end of it.

trotyl added a commit to trotyl/angular that referenced this issue Feb 21, 2018
@pkozlowski-opensource pkozlowski-opensource changed the title RegExp in templates for pipes RegExp literals in expressions support Mar 9, 2019
@pkozlowski-opensource pkozlowski-opensource added the core: binding & interpolation Issue related to property/attribute binding or text interpolation label Mar 17, 2020
@mhevery
Copy link
Contributor

mhevery commented Dec 2, 2020

We don't think this aligns with our vision for Angular. The main concern is that this will further complicate the syntax and require maintenance from the Angular team. Creating RegExp is relatively easy in component and it than can be used from template.

@mhevery mhevery closed this as completed Dec 2, 2020
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jan 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime core: binding & interpolation Issue related to property/attribute binding or text interpolation feature Issue that requests a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants