-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Comments
Duplicate of #6419 |
@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 |
I can act as compiler people as long as they're willing to have it. 😃 |
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. |
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. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
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
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
The text was updated successfully, but these errors were encountered: