Today I was working on some code where an error message could follow one of three basic formats:
[Column Name] is required to be a/n [data type]
[Column Name] is a required field.
[Column Name] must be a/n valid [data type]
The trouble was that [Column Name] isn't known (only the entire message), and doesn't come in the case (as in upper/lower) that I want it displayed. Therefore, I wanted to use
rereplace()
to end with something like:
<span style='text-transform: capitalize;'>#columnName#</span> [rest of message].
Of course, this isn't very hard. You just need to use a backreference to substitute the old match into the replacement substring:
rereplace(err, "((is[ a]*required)|(must be))" ,"</span>\1","one")
.
The
\1
is the backreference. But, the part that
I had forgotten is that you need parenthesis around the matched text that you are referencing. Therefore, all I needed was the outer parentheses.
I know this is actually quite simple, and mostly useless for most people, but I hadn't used it in a while, and forgot how to do it. So, I thought I'd post a little reminder for myself, and anyone else who might need it.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
There are no comments for this entry yet.
Leave a comment