using nodejs I am trying to generate an unique URL for user to conform email address. From that URL user will be able to verify th
However, .replace()
will only replace first occurrence.
To be more precise, you can use something like this :
// Original Text
var ciphertext = 'asda+dasd+asdas/asdasd/sadasdasd/dadasd=adsasda=dasd=';
// Replaced Text
var dataString = ciphertext.replace(/\+/g,'p1L2u3S').replace(/\//g,'s1L2a3S4h').replace(/=/g,'e1Q2u3A4l');
console.log(dataString);
// Back to Original Text
ciphertext = dataString.replace(/p1L2u3S/g, '+' ).replace(/s1L2a3S4h/g, '/').replace(/e1Q2u3A4l/g, '=');
console.log(ciphertext);
IMHO, for @JaiKumarRajput's answer,
He encoded the string with,
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
Now, I have no idea how xMl3Jk
, Por21Ld
, Ml32
works. So, i also don't know if it can mess my string somehow.
Plus, As I have to perform this on decoding as well. So, Why wont I use something like this (What already exists),
encodeURIComponent(ciphertext.toString('base64'))
I know it still introduces %
char. But as its getting used in URL. In which its a escape char.
How does it matter more than doing something that can mess my code up ??
NOTE: I used it and had no issue, It doesn't mean I either had found any issue with the top one. That didn't feel neat. Its only my humble opinion, So if u don't like it? Ignore it.
You can easily replace the special characters with any of text like:
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
Do not forget to replace these strings back with special characters
dataString.toString().replace('xMl3Jk', '+' ).replace('Por21Ld', '/').replace('Ml32', '=');
Hope this will help to solve your problem