xsl translate function in javascript

J

jeanph01

Hi!
How to do the same thing in javascript as this xsl function :

translate(@name,"àâÂ","aaa")


The only thing i can think of is a chain of replace.
ex:
chain.replace("à","a").replace("â","a").replace("Â","a");

Thank you !
 
D

Dr J R Stockton

In comp.lang.javascript message <6ccf4c05-57db-47df-bdee-af6fb7d2d67a@d4
g2000vbm.googlegroups.com>, Mon, 3 Aug 2009 11:58:18, jeanph01
Hi!
How to do the same thing in javascript as this xsl function :

translate(@name,"àâÂ","aaa")


The only thing i can think of is a chain of replace.
ex:
chain.replace("à","a").replace("â","a").replace("Â","a");

For the general case,

Arr = []
for (J=0 ; J<65536 ; J++) Arr[J] = String.fromCharCode(J)
Arr[233] = "E" // replace é with E
Arr[0xFC] = "U" // replace ü with U

S1 = "foréign stüff"
S2 = ""
for (J=0 ; J<S1.length ; J++) S2 += Arr[S1.charCodeAt(J)]

Every character is unchanged in the copying, except for characters which
have been changed in the array Arr.
 
T

Thomas 'PointedEars' Lahn

Dr said:
jeanph01 posted:
How to do the same thing in javascript as this xsl function :

translate(@name,"àâÂ","aaa")


The only thing i can think of is a chain of replace.
ex:
chain.replace("à","a").replace("â","a").replace("Â","a");

For the general case,

Arr = []
for (J=0 ; J<65536 ; J++) Arr[J] = String.fromCharCode(J)
Arr[233] = "E" // replace é with E
Arr[0xFC] = "U" // replace ü with U

S1 = "foréign stüff"
S2 = ""
for (J=0 ; J<S1.length ; J++) S2 += Arr[S1.charCodeAt(J)]

Every character is unchanged in the copying, except for characters which
have been changed in the array Arr.

There is no need for this error-prone, inefficient approach. Where
String.fromCharCode() and String.prototype.charCodeAt(), or Array
initializers, are supported, String.prototype.replace() is supported
and can take a RegExp object reference as first argument. Cf. kangax's
solution.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Tue,
4 Aug 2009 20:47:26 said:
Dr said:
jeanph01 posted:
How to do the same thing in javascript as this xsl function :

translate(@name,"àâÂ","aaa")


The only thing i can think of is a chain of replace.
ex:
chain.replace("à","a").replace("â","a").replace("Â","a");

For the general case,

Arr = []
for (J=0 ; J<65536 ; J++) Arr[J] = String.fromCharCode(J)
Arr[233] = "E" // replace é with E
Arr[0xFC] = "U" // replace ü with U

S1 = "foréign stüff"
S2 = ""
for (J=0 ; J<S1.length ; J++) S2 += Arr[S1.charCodeAt(J)]

Every character is unchanged in the copying, except for characters which
have been changed in the array Arr.

There is no need for this error-prone, inefficient approach. Where
String.fromCharCode() and String.prototype.charCodeAt(), or Array
initializers, are supported, String.prototype.replace() is supported
and can take a RegExp object reference as first argument. Cf. kangax's
solution.

Evidently you have, as usual, not been thinking. In the general case, a
considerable number of different characters will each need to be
replaced by some other character, and those other characters will not
all be the same. That could be done by a chain of RegExps, one for each
of the replacement characters, which would be at best inelegant. Or it
could be done with a single RegExp using a complicated function to do
the detail.

The OP wished to replace a variety of accented letters A, in both cases,
with lower-case a. ISTM very probable that he will want to do likewise
with all the other accented letters that might appear; perhaps more than
are in any one natural language.

Kangax's solution is of course good for the question as asked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top