• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need regex to help escape string with &.

aceO07

Diamond Member
I need to escape strings that have the ampersand character and replace it with &. Basically things like • should be •.

What's the regex to look for '&' then some other characters followed by ';' but not &. I want to look for all those instances and replace only the beginning & with &
 
You don't want to replace all ampersands with &? I'd do that like this:

s/\&/\&/g

Escapes ('\'s) vary by language, but & in the replacement string frequently refers to the original match. Though I guess that doesn't really make a difference here.

Oh, I see, you don't want to replace & with &. Support for zero-width negative look-ahead assertions varies from language to language. s/\&(?!amp😉/\&/g should work in Perl, but may not in other languages. If that's not possible, I'd go ahead and make those &s, then use a second regex to get rid of them: s/\&/\&/g
 
Last edited:
You don't want to replace all ampersands with &? I'd do that like this:

s/\&/\&/g

Escapes ('\'s) vary by language, but & in the replacement string frequently refers to the original match. Though I guess that doesn't really make a difference here.

Oh, I see, you don't want to replace & with &. Support for zero-width negative look-ahead assertions varies from language to language. s/\&(?!amp😉/\&/g should work in Perl, but may not in other languages. If that's not possible, I'd go ahead and make those &s, then use a second regex to get rid of them: s/\&/\&/g

Thanks. That last bit is a good idea too. I actually ended up writing some short code to do it a short while after my post. It doesn't get run too often, so it isn't a big issue.
 
Back
Top