2

I'm trying to replace the "'" character with the "''" string using the replace method, like this:

temp.replace("\'", "''");

But it seems that it is not replacing anything. How should I write the command in order to replace the wanted character with the wanted string? (I'm trying to replace it in the following XML file.)

<Module ForceTabs="1">
    <Events>
        <Event Value="onafter_moduleinit_beforedraw()" Type="onafter_moduleinit_beforedraw"/>
    </Events>
    <Section Id="Header" Type="header" Caption="Header" ContainerCSS="background:#C1E0F4;border-bottom:2px groove;margin-bottom:3px;padding:10 3 3 3">
        <Containers>
            <Form Id="Header" Caption="General Details" Source="Request" Layout="Fixed">
                <Layout>
                    <table cellaspacing="0" cellpadding="0">
                        <tr>
                            <td>
                                <ControlRef Id="Origin" ShowCaption="1"/>
                            </td>
                            <td>
                                <ControlRef Id="Type" ShowCaption="1"/>
                            </td>
                            <td>
                                <ControlRef Id="Process" ShowCaption="1"/>
                            </td>
                            <td width="20" align="center">
                                <span class="separator"/>
                            </td>
                            <td>
                                <ControlRef Id="FindBy" ShowCaption="1"/>
                            </td>
                            <td>
                                <ControlRef Id="Find"/>
                            </td>
                            <td>
                                <button width="30" onclick="doFindCustomer()">
                                    <Caption Caption="GO"/>
                                </button>
                            </td>
                        </tr>
                    </table>
                </Layout>
                <Controls>
                    <Control Id="Origin" Caption="Origin" FieldName="Origin" DataType="string" Width="90" CaptionWidth="70" ReadOnly="1" Mandatory="1" Hidden="1"/>
                    <Control Id="Type" Caption="Type" FieldName="Type" DataType="select" Width="60" CaptionWidth="40" ReadOnly="1" Mandatory="1" DefaultValue="Service" Hidden="1">
                        <Member Value="Allert" Caption="Allert"/>
                        <Member Value="Service" Caption="Service"/>
                        <Member Value="Marketing" Caption="Marketing"/>
                    </Control>
                    <Control Id="Process" Caption="Process" FieldName="Process" DataType="string" Width="90" CaptionWidth="70" ReadOnly="1" Mandatory="1" Hidden="1"/>
                    <Control Id="FindBy" Caption="Find:" VCaption="Find By" FieldName="FindBy" DataType="select" Width="90" CaptionWidth="50" ReadOnly="0" Mandatory="0" CaptionCSS="font-weight:bold" DefaultValue="IMTSI">
                        <Member Value="IMTSI" Caption="IMTSI"/>
                        <Member Value="ID" Caption="ID"/>
                    </Control>
                    <Control Id="Find" Caption="Find Value" FieldName="Find" DataType="string" Width="90" CaptionWidth="60" ReadOnly="0" Mandatory="0" CaptionCSS="font-weight:bold"/>
                </Controls>
            </Form>
        </Containers>
    </Section>
    <Section Id="1" Type="page" Caption="Message Details">
        <Containers>
            <Form Id="General" Caption="General Details" Source="Request" Layout="Fixed">
                <Controls>
                    <Control Id="MessageType" Caption="Message Type" Source="Param[@Name = 'MessageType']" FieldName="Value" DataType="select" Width="150" CaptionWidth="120" ReadOnly="1" Mandatory="1" DefaultValue="Allert" Action="handleMessageTypeChange()" BreakAfter="0">
                        <Member Value="SMS" Caption="SMS"/>
                        <Member Value="EMAIL" Caption="EMAIL"/>
                        <Member Value="ATOS" Caption="ATOS"/>
                        <Member Value="SELF SERVICE" Caption="SELF SERVICE"/>
                    </Control>
                    <Control Id="Language" Caption="Language" Source="Param[@Name = 'Language']" FieldName="Value" DataType="string" Width="100" CaptionWidth="90" ReadOnly="1" BreakAfter="1"/>
                    <Control Id="FirstName" Caption="First Name" Source="Param[@Name = 'FirstName']" FieldName="Value" DataType="string" Width="350" CaptionWidth="120" BreakAfter="1"/>
                    <Control Id="LastName" Caption="Last Name" Source="Param[@Name = 'LastName']" FieldName="Value" DataType="string" Width="350" CaptionWidth="120" BreakAfter="1"/>
                    <Control Id="IMTSI" Caption="IMTSI" Source="Param[@Name = 'IMTSI']" FieldName="Value" DataType="string" Width="350" CaptionWidth="120" BreakAfter="1" Mandatory="0"/>
                    <Control Id="Mobile" Caption="Mobile" Source="Param[@Name = 'Mobile']" FieldName="Value" DataType="string" Width="350" CaptionWidth="120" BreakAfter="1" Mandatory="1"/>
                    <Control Id="Email" Caption="Email" Source="Param[@Name = 'Email']" FieldName="Value" DataType="string" Width="350" CaptionWidth="120" BreakAfter="1" Mandatory="0"/>
                    <Control Id="Subject" Caption="Subject" Source="Param[@Name = 'Subject']" FieldName="Value" DataType="string" Width="350" CaptionWidth="120" BreakAfter="1" MaxLen="40" Hidden="1"/>
                    <Control Id="Content" Caption="Content" Source="Param[@Name = 'Content']" FieldName="Value" DataType="longstring" Width="350" CaptionWidth="120" Height="60" BreakAfter="1" MaxLen="50" Mandatory="1" SeparateBefore="1" SeparateAfter="1"/>
                    <Control Id="Sender" Caption="Sender" Source="Param[@Name = 'Sender']" FieldName="Value" DataType="string" Width="350" CaptionWidth="120" ReadOnly="1" BreakAfter="1"/>
                    <Control Id="Priority" Caption="Priority" Source="Param[@Name = 'Priority']" FieldName="Value" DataType="select" Width="350" CaptionWidth="120" BreakAfter="1" DefaultValue="LOW">
                        <Member Value="LOW" Caption="Low" Glyf="../../GUIGenerator_V2/assets/themes/Images/flag_blue.png"/>
                        <Member Value="MED" Caption="Medium" Glyf="../../GUIGenerator_V2/assets/themes/Images/flag_yellow.png"/>
                        <Member Value="HIGH" Caption="High" Glyf="../../GUIGenerator_V2/assets/themes/Images/flag_red.png"/>
                    </Control>
                </Controls>
            </Form>
        </Containers>
    </Section>
</Module>

I must replace the single quote to two single quotes. I can't change it to ", but to ''.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1176926
  • 249
  • 3
  • 9
  • 17

7 Answers7

18

Your code just replaces a single instance (the first one it finds). You should replace all instances. You can do this by using a regular expression and adding a g flag to the end meaning "global search". Like this:

temp.replace(/'/g, "''")

Here's a working example: http://jsfiddle.net/Q2Uyv/ (type something into the "In" box and click "Convert").

If you are actually trying to replace single quotes with double quotes (instead of doubled sequence of single quotes), do this:

temp.replace(/'/g, '"')
Ben Lee
  • 52,489
  • 13
  • 125
  • 145
  • @user1176926, I added a different working example to my answer where you can enter any string to confirm that it works. – Ben Lee Feb 29 '12 at 12:36
  • 3
    @BenLee I hope the OP reconsiders and selects this answer, as this one is by far the best one. – Zoidberg Feb 29 '12 at 12:51
  • @Zoidberg, I don't even care about the 15 rep or accepted answer, I'm just appalled that the worst alternative was chosen as accepted when every other answer is more acceptable. I'd be happy if anyone else with a working answer got the checkmark. (Okay, I admit "appalled" is way too strong a word, but still...) – Ben Lee Feb 29 '12 at 16:48
  • @BenLee Yes, that answer is definitely the worse, and it really isn't about the rep so much as THAT bad answer coming up as the accepted solution when someone is searching for a way to solve this problem. – Zoidberg Mar 01 '12 at 03:31
6

The problem is that

temp.replace("\'", "''");

will only replace the first instance of '. To fix this, do the following instead

temp.replace(/'/g, "''"));

This will ensure it goes though and replaces all instances of the single quote instead of just the first.

Zoidberg
  • 10,137
  • 2
  • 31
  • 53
2

The trick is quoting each string with the other quote character:

temp.replace(/'/g, '"');

Ben Lee is correct about the regex. However, I still gather it that you want to replace with " (one double quote), not '' (two single quotes).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
2

You're actually trying to replace ('), not just the single quote character. There is no need to escape a single quote in a string delimited by double quotes, because they don't signify the end of a string...

Replace will only replace the first quote matched, unless you're using our old friend the regular expression. The downside being regular expressions tend to be slow.

I haven't checked to see which is faster, but you could try using split:

var chunks = temp.split("'").join("''");

test = "string cont'aining single 'quotes".split("'").join("''"); // In the Firebug console
// returns "string cont''aining single ''quotes" as expected
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

Simply try this:

temp.replace("'", "''");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

It's basically changing the single quote character with two single quote characters, right? If that's the case, you might want to use the global flag, g at the end of your regular expression and assign it back to your value (temp):

temp = temp.replace(/'/g,"''");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
inhan
  • 7,394
  • 2
  • 24
  • 35
-5
while (str.indexOf("'") >= 0)
    {
        var newStr = str.replace("'", "\"");
        str = newStr;
    }
gabitzish
  • 9,535
  • 7
  • 44
  • 65
  • I have now another problem, I'm trying to save that xml in a mssql, but getting an error message, what could be the problem? – user1176926 Feb 29 '12 at 12:46
  • 4
    Why this answer as opposed all the other better ones? Also, if you want to replace single quotes with double quotes, why does your original code in the question have two single quotes in the second part of the replace? – Zoidberg Feb 29 '12 at 12:49
  • 7
    Wow, this was accepted? This is the *worst* of all the working alternatives presented. It is both the most ugly and the least efficient. I just hope nobody else who has this problem and comes along to to this page sees this and thinks it's the right way to do it without looking at the other answers. – Ben Lee Feb 29 '12 at 16:27