Editing template variables using regularExpression()
I'm building a template for web components whose class names should depend on the file name. In practice:
prefix-component-name.tsx
should return
ComponentName
Now, I was able to return the desired component name including the prefix with the following expression:
capitalize(camelCase(fileNameWithoutExtension()))
I though in order to strip the prefix I could use a regular expression:
regularExpression(camelCase(fileNameWithoutExtension()), ^.+[-], "")
However, it obviously didn't work. I couldn't find any example about regularExpression(String, Pattern, Replacement) usage in the docs. Any help please?
Please sign in to leave a comment.
Please see comments in https://youtrack.jetbrains.com/issue/IDEA-146121.
Function implementation: https://github.com/JetBrains/intellij-community/blob/master/platform/lang-impl/src/com/intellij/codeInsight/template/macro/RegExMacro.java
Thank you @Elena!
I had to wrap the pattern in quotes to make it work:
I tried many ways and cannot get this to work.
Currently running IntelliJ 2020.3.2 Ultimate.
Examples (that do NOT work):
works fine for me
I'm not sure what happened. After a restart it seems to work.
Maybe something crashed internally. I tried to get this working for about an hour and tried every possible way it could think of.
The position where this regex should show up, always ended up being empty.
Can I use capture group? How will I do?
regularExpression(String, Pattern, Replacement)
Live Template function is implemented as JavaString.replaceAll(Pattern, Replacement)
. Please refer to java Pattern documentation for information on the supported syntax