3

Hopefully you have some experience with visual studio code snippet writing if you have opened this and you can help me.

I am trying to get better at writing visual studio code snippets.

This is one I have at the moment:

"Styled Template": {
 "prefix": "sty",
 "body": [
 "import styled from \"styled-components\";",
 "",
 "const colors = (props) => props.theme.colors.${TM_FILENAME_BASE/(.*)/${1:/downcase}/};",
 "",
 "export const Container = styled.div`",
 " display: flex;",
 " width: 100%;",
 " height:100%;",
 "`;",
 "2ドル"
 ],
 "description": "Styled Template"
 },

As you can see above I am using the filename base contant in my snippet and I am transforming the text to be downcase but I also need to transform it with another regex so replace the text '.styled' in the name with nothing ''.

Is it possible to add 2 transforms on the same element? I am struggling to find a way at the moment.

asked Sep 19, 2020 at 13:14
2
  • 1
    Try ${TM_FILENAME_BASE/^(?:(.*?)(?:\.styled))?(.*)$/${1:/downcase}${2:/downcase}/} Commented Sep 19, 2020 at 13:17
  • amazing..it works! thanks a lot!! Commented Sep 19, 2020 at 13:21

1 Answer 1

2

You can use

${TM_FILENAME_BASE/^(?:(.*?)(?:\.styled))?(.*)$/${1:/downcase}${2:/downcase}/}

See the regex demo

Pattern details

  • ^ - start of string
  • (?:(.*?)(?:\.styled))? - an optional occurrence of:
    • (.*?) - Group 1: any zero or more chars other than line break chars, as few as possible
    • (?:\.styled) - a .styled substring
  • (.*) - Group 2: any zero or more chars other than line break chars, as many as possible
  • $ - end of string.

So, in this case, the part before .styled is captured into Group 1 and the part after it is captured in Group 2. The replacement is a concatenation of these two groups (with /downcase applied to both).

answered Sep 19, 2020 at 13:22
Sign up to request clarification or add additional context in comments.

3 Comments

gosh I wish I knew regex to at least this level, gotta spend more time learning it. Thanks a lot for this great response.
@TalmacelMarianSilviu I explained it in brief at the bottom of the answer. The point here is to capture the text before and after .styled if this string is present. Then, the two captured substrings are concatenated in the replacement. Since the whole string is matched with the regex, and the replacement does not contain .styled, this substring is removed.
yes I've understand it after I asked the questions ha ha..thats why I deleted it. thanks again!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.