We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Detect if field contains regex match, and if so, do something with that match

How Do I?

rtixweb's avatar
rtixweb
12 posts
one year ago
rtixweb's avatar rtixweb

If a file URL variable has a pattern that looks like pound sign + number, I want to know that and act differently. In that case I also need to pull the number out and insert it into a URL.

My pseudo code would look something like this:

{if file_link matches regex /#%d/ }

<td><a href="http://someotherurl.com/{file_link_regex_result}">{file_link_title}</a></td>

{if:else}

<td><a href="http://{file_link}">{file_link_title}</a></td>

{/if}

` How can I accomplish this?

       
Rob Allen's avatar
Rob Allen
3,102 posts
one year ago
Rob Allen's avatar Rob Allen

Would the replace variable modifier work here? https://docs.expressionengine.com/latest/templates/variable-modifiers.html#replace

       
rtixweb's avatar
rtixweb
12 posts
one year ago
rtixweb's avatar rtixweb

I see now that I can detect the pound sign plus number with the ~ operator as shown below, but I’m still not sure how to then extract the number for use in my URL.

{if title ~ "/#[ ]*\d+/" }

       
rtixweb's avatar
rtixweb
12 posts
one year ago
rtixweb's avatar rtixweb

Thank you, Rob, that did the trick. Since my number always came at the end, I was able to delete everything from the start to the # character with this code:

{title:replace find='/.*#[ ]*/' replace='' regex='yes'}

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.