简体   繁体   中英

RegEx That Matches Curly Braces and Leading or Trailing Substr Beginning or Ending with Quotes

I have a regex that I am using to match various pairs of curly braces so I can comment them out during development.

My RegEx is:

/{{.+_includes\s*}}|[^>\s]*{{\s*|\s*}}[^<\s]*|{%.+%}/g

And this matches the substrings I've wrapped in # signs:

1.  #{{ #builtin_body_classes# }}#  

2.  #"{{ #builtin_body_classes# }}# no-js"  

3.  "no-js #{{ #builtin_body_classes# }}"#

4.  "no-js some-other-class #{{#builtin_body_classes#}}"#

5.  #{{standard_header_includes}}#

6.  #{{ standard_footer_includes }}#

7.  <title>#{{ #page_meta.html_title# }}#</title>

8.  #{% type_of_module "unique_module_name" parameterString='String parameter value', parmeterBoolean=True %}#

Note the whitespace (two spaces) the lead and trail each line are not matched and nor are the example title tags.

This works exactly as I want for all but two cases — the lines that are wrapped in quotes (cases 2, 3 and 4).

For these cases I would like the quotes and substrings that lead or trail the curly braces also included in the match eg

2.  #"{{ #builtin_body_classes# }} no-js"#  

3.  #"no-js {{ #builtin_body_classes# }}"#

4.  #"no-js some-other-class {{#builtin_body_classes#}}"#

Again, I've wrapped the substrings I would like to match between # signs.

Would very much appreciate help to achieve this, and I'm sure my regex could be optimised too ;-)

I've now solved the problem. My regex is:

/{{.+_includes\s*}}|(?:"+.*|[^>\s]*){{\s*|\s*}}(?:.*"+|[^<\s]*)|{%.+%}/g

You can see my matches below and also live at http://regexr.com/3fijm

1.  #{{ #builtin_body_classes# }}#  

2.  #"{{ #builtin_body_classes# }} no-js"#  

3.  #"no-js {{ #builtin_body_classes# }}"#

4.  #"no-js some-other-class {{#builtin_body_classes#}}"#

5.  #{{standard_header_includes}}#

6.  #{{ standard_footer_includes }}#

7.  <title>#{{ #page_meta.html_title# }}#</title>

8.  #{% type_of_module "unique_module_name" parameterString='String parameter value', parmeterBoolean=True %}# 

As I said in my original question I am sure this regex could be optimised or improved. So suggestions welcome!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM