1

I am trying to remove accents from a string using REreplace but it is just not working.

Here is the string:

Alertas del Condado: Por favor, háganos saber si usted recibe este mensaje. Gracias.

Trying to remove the accent in "háganos" and replace it with "a".

Here is the code:

Rereplace(trans_sms, 'á', 'a', 'all')

Where trans_sms is the string.

I have been trying to search but cannot figure out why it is not replacing.

Thank you in advance for anyone's help!

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
Scott
  • 459
  • 2
  • 10
  • 2
    The "RE" at the beginning of the function you're using stands for "regular expression", however you are not providing a regular expression. If you want to replace one substring with another, use the "replace()" function. You can test it and see it work at https://cffiddle.org However, Adrian's answer is more efficient. – EddieLotter Apr 19 '23 at 03:06
  • Out of interest, why are you trying to do this? – Adam Cameron Apr 19 '23 at 06:07
  • 1
    Voting to reopen this question as the linked post has an outdated and far less performant approach to the problem this poster is trying to address. – Adrian J. Moreno Apr 19 '23 at 16:47
  • Agreed, also voted to reopen and... here we are... reopened. Also untagging "regex" as it's nowt to do with regular expressions. – Adam Cameron Apr 19 '23 at 16:54
  • Don't forget that strings are immutable, so you'll need to set the variable to the replaced version: `trans_sms = replace(trans_sms, 'á', 'a', 'all')` – mykaf Apr 20 '23 at 19:19

1 Answers1

5

You don't use ColdFusion, you use Java.

Apache Commons StringUtils, function stripAccents

<cfscript>
    stringUtils = new java("org.apache.commons.lang3.StringUtils");
    accents = "á,é,í,ó,ú,ý,à,è,ì,ò,ù,â,ê,î,ô,û,ã,ñ,õ,ä,ë,ï,ö,ü,ÿ,À,È,Ì,Ò,Ù,Á,É,Í,Ó,Ú,Ý,Â,Ê,Î,Ô,Û,Ã,Ñ,Õ,Ä,Ë,Ï,Ö,Ü,x";
    simple = stringUtils.stripAccents(accents);
    writeOutput(accents & " <br> " & simple);
</cfscript>

Output:

á,é,í,ó,ú,ý,à,è,ì,ò,ù,â,ê,î,ô,û,ã,ñ,õ,ä,ë,ï,ö,ü,ÿ,À,È,Ì,Ò,Ù,Á,É,Í,Ó,Ú,Ý,Â,Ê,Î,Ô,Û,Ã,Ñ,Õ,Ä,Ë,Ï,Ö,Ü,x
a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u,a,n,o,a,e,i,o,u,y,A,E,I,O,U,A,E,I,O,U,Y,A,E,I,O,U,A,N,O,A,E,I,O,U,x
Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
  • This is a better answer than the accepted one on the Q that this was marked as a duplicate of – Adam Cameron Apr 19 '23 at 09:00
  • Thank you very much for the feedback. We are doing this because when we send the string through another network, it replaces all accent chars with "?". As far as the script above, if I was using a var string called "trans_sms" how would I gat that to replace any thing in the string that is in the accents var with what's in the simple var above? Thank you all again, this truly is helping. – Scott Apr 19 '23 at 20:39
  • Whatever your variable is that contains the accented string, pass that variable to the function to get back the whole string as plain ASCII characters. – Adrian J. Moreno Apr 19 '23 at 22:14
  • Great solutions! But doesnt works on Lucee 5. On Lucee 6 does. – Roberto Jul 19 '23 at 07:14