0

Is it possible to change the case AND change spaces to underscores using Low Variables?

Basically I was trying to use...

{exp:low_replace find='\s+|\w+' replace='_|\l+' regex='yes' multiple='yes'}Flavours yo{/exp:low_replace}

But I tihnk I may have got the RegEx completly wrong (Not my strong point!)

Because this just outputs

L

0

First off, I don't think Low Replace supports multiple="yes" for regex replacements. Considering the complexities allowed in regex expressions, it would be a difficult task indeed to allow Replace A|B|C with X|Y|Z type regex searches when each of A|B|C are individual regex searches.

I also don't see any Low Replace functionality to do the replace with case transforms. In straight regex it's pretty simple: prefix your replacement with \U (or something very similar, depending on your regex flavor), but Low's only implementing a regex subset, it seems. No matter the syntax I tried, I was not able to get the replacement to change case. Of course, I could be missing something obvious, wouldn't be the first time (see some of my questions here on the Stacks. ;-) If the resulting text does not actually have to BE uppercase, but can rather just look uppercase, then how about a little CSS text-transform:uppercase?

So, we're left with the basic replacement of spaces to underscores. That's achievable with

{exp:low_replace find="([\w\W+]) +" replace="$1_" regex="yes"}
  Lorem ipsum dolor sit amet. Nearly.
{/exp:low_replace}

You will note that the search is for both alpha-numeric characters (\w) as well as non-alpha-numerics (\W). This is to factor for the possibilities of commas, periods, etc. If you know that your case will never encounter these, you can leave the statement as

{exp:low_replace find="(\w+) +" replace="$1_" regex="yes"}
  Lorem ipsum dolor sit amet. Nearly.
{/exp:low_replace}

The first statement results in

Lorem_ipsum_dolor_sit_amet._Nearly.

The second statement results in

Lorem_ipsum_dolor_sit_amet. Nearly.

(note the missing underscore before 'Nearly.')

Now, for what it's worth, there's a utility by Causing Effect by the name of CE String that just completely kicks ninja string manipulation arse. For $10 it's a Bat Utility Belt of string manipulation the likes of which are endlessly joyous for a nerd. Worth a look. The beauty of it is that you can pile up your manipulations, so, say, do your regex search/replace, then UPPERCASE the whole thing.

FYI.

  • Wow, thanks @dashard amazing reply. Sadly I can't use CSS for the case change, it's not for the styling, but more for some form names. I'll try CE String, I have used this in the past but always fall back to Low Replace. Thanks. – Jason Mayo Jun 20 '14 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.