javascript replace multiple letters

Hi. In order to pass parameters in AJAX, when calling function from js file, special characters like /  ' '(space)  ,  #  , ... they have to be encoded.

I use

codigo = codigo.toUpperCase().replace(/[ ]/g, "_").replace(/[#]/g, "NN").replace(/[/]/g, "ZZZZZZZ")

to make these replacements:

(space) ==> _
# ==> "NN"
/ ==> >"ZZZZZZZ"

I have other procedure I get these names as IDS, so I need to decode these values back, but for example, I use this to decode it:

xcodss = xcods.substring(0,nlen).replace(/["NN"]/g,"#").replace(/[_]/g, " ").replace(/["ZZZZZZZ"]/g, "/");
but where I have "Z" , i get / in return
  where I would be expecting only one / if I had 7 Zs (ZZZZZZZ)

for example, if I have "N", then I get #
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aguisaAuthor Commented:
ok, the reason is that I'm using the code of an item as the ID of the javascript object.

for example, an Item can have the code:    "#10", so I assign the alias: NN10, and name the object representing this item on the ID='NN10'
so, now in Javascript I can do things like:


The thing is that if I didn't do the replacement, then in Javascript, I would have things like:

$("##10").val(....) which makes some errors, also lets say another code:   "1034 12V"

note the space, now back in JS
, things like  

$("#1034 12V").html(...) would also cause errors, contrary to


I appreciate the very good first recommendation, but its falls from being the best approach in my case, lets say I use it, then again, in JS things like (for the item with code= "#10", i.e.)
$("#%2310).html(...) Js dont like.

please correct me. thanks
When you escape special characters, the server receive them unescaped. So when you get the characters back in the browser you get them in the original format (unescaped)
Try changing:

  xcodss = xcods.substring(0,nlen).replace(/["NN"]/g,"#").replace(/[_]/g, " ").replace(/["ZZZZZZZ"]/g, "/");


  xcodss = xcods.substring(0,nlen).replace(/NN/g,"#").replace(/_/g, " ").replace(/ZZZZZZZ/g, "/");

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.