Home > Net >  How to grab last 3 characters of a string that contains a given substring
How to grab last 3 characters of a string that contains a given substring

Time:11-07

I wondered how I could only grab the last 3 chars of a string only containing a given substring. For example, I want to grab every -%} that does have the substring "assign" or "liquid" preceding it. In this case, I'd only want the -%} from lines 1, 2, and 8.

1 {%- assign weather = rain -%}
2 {%- assign num_apples = 10 -%}

3 {%- render 'thing' -%}

4 {%- liquid  
5   if true
6     return
7   endif 
8  -%}

Note that this is being used for a find and replace. I'm using ShellJS if that helps.

I can't seem to get closer than these:

(?=assign)*.{3}$

(?=(?<=assign )).*

(?={%- assign )(.*){3}$

CodePudding user response:

This works well with JS.

(?<={%-\s*?(?:assign|liquid)\s[^{}]*?(?=-%}))-%}

See live demo.

  • Related