I'm using an editor driven by Smarty template engine.
I have data that is passed into $productName via an API that produces a string which looks like this:
Hills Portable 170 Clothesline,Versaline Broadline Clothesline,Austral Addaline 35 Clothesline,Hills Rotary 8 Clothesline
What I need is to only show the first product from this string that is from the Hills Brand (Brands above are Hills, Versaline, Austral).
The template syntax allows two parameters to be passed in after the command regex_replace:
The first is the regular expression to be replaced and the second is the string of text to replace it with.
Here's the code:
{$productName|regex_replace:" ":" "}
What I need help with is specifying the actual regular expression so that anything in between "Hills" and the comma that separates it from the next product name remains, and everything else is removed.
Current output: Hills Portable 170 Clothesline,Versaline Broadline Clothesline,Austral Addaline 35 Clothesline,Hills Rotary 8 Clothesline
Desired output: Hills Portable 170 Clothesline
Here's the documentation on regex_replace from Smarty: http://www.smarty.net/docsv2/en/language.modifier.regex.replace.tpl
Thanks in advance!
Regular expressions can be complicated, but in this case it's fairly simply.
{$productName|regex_replace:"/,.+/":""}
This expression captures a comma, followed by anything else and replaces it with nothing.
You can 1) split the string first and put into array
var str = "How are , doing today?"; var res = str.split(",");
res is a array and just pick the desired index