anirudhkumar_r has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
How are you doing today? I am in a little trouble and need your help with regex.
I am replacing some strings in JavaScript. However, I believe the same regex will work for both JavaScript and Perl.
With my method of replacing, I am getting into the problem of recursive replacement.
I want to do the following.
1. Replace & with &
2. Replace &lt; with <
3. Replace &gt; with >
4. Replace < with <
5. Replace > with >
I tried to include the following code snippets in my JS code.
But the problem with the above code snippet is, both text segments like [<] and [&lt;] will be replaced ultimately with [<] symbol. Is it possible to include all the three replacements in a single statement to avoid recursive replacement?new_json_string = new_json_string.replace(/&/g, '&'); new_json_string = new_json_string.replace(/</g, '<'); new_json_string = new_json_string.replace(/>/g, '>');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Avoid recursive replacement using regex
by parv (Parson) on Nov 25, 2014 at 11:34 UTC | |
|
Re: Avoid recursive replacement using regex
by Athanasius (Archbishop) on Nov 25, 2014 at 12:09 UTC | |
|
Re: Avoid recursive replacement using regex
by AnomalousMonk (Archbishop) on Nov 25, 2014 at 14:34 UTC | |
|
Re: Avoid recursive replacement using regex
by Loops (Curate) on Nov 25, 2014 at 12:39 UTC |