I have an interesting question about string and delimiters.
There is a random string
There are some symbols (OR SEQUENCES OF SYMBOLS) that are predefined and should be temporarily replaced within that string by
var str1 = str.replace(/abc/g, 'newchar1').replace(/,/g, 'newchar2'); // which replaces
abcoccurencies and commas with
After some computations I do the "reverse" replacements by doing
str1.replace(/newchar1/g, 'abc').replace(/newchar2/g, ',');
and expect to have the same number of abc and commas and in the same places as before any changes.
As you understand there are problems with this method:
newchar1 may exist in the string before. and that's a problem.
That way I need to create a random hash for each delimiter (let's say 4-character random string) and check first it is not a part of an original string.
Moreover, the hashes for each delimiter should differ from each other.
Moreover, the hashes should not be the part of each other. Otherwise see what happens, if ("aa", "ac", "cd') - 3 generated hashes for ("abc", ",", "W") respectively. Good enough? Well.. Let's see... See what bad happens with this
aabcW,kkk ====step1====> aaacdackkk ====step2====> abc,d,kkk.Oh, unexpected result - different number of commas, lost info.
Do you see the problem?
So, what's the algorithm to properly generate, say, five hashes to replace five substrings within a string and making the reverse operation as described above without losing any info?