替换“'"用“''"在JavaScript中

Replace " ' " with " '' " in JavaScript

本文关键字:quot JavaScript 替换      更新时间:2023-09-26

我正在尝试替换"CCD_ 1";带有";CCD_ 2";字符串使用replace方法,如下所示:

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

但它似乎并没有取代任何东西。我应该如何编写命令,以便用所需的字符串替换所需的字符?(我试图在下面的XML文件中替换它。)

<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>

我必须将单引号替换为两个单引号。我不能把它改成",而是改成''

您的代码只替换了一个实例(它找到的第一个实例)。您应该替换所有实例。您可以通过使用正则表达式并在末尾添加g标志来实现这一点,该标志表示"全局搜索"。像这样:

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

下面是一个工作示例:http://jsfiddle.net/Q2Uyv/(在"输入"框中键入内容,然后单击"转换")。

如果你真的试图用双引号代替单引号(而不是双引号序列),请这样做:

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

问题是

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

将只替换"的第一个实例。要解决此问题,请执行以下操作,而不是

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

这将确保它通过并替换单引号的所有实例,而不仅仅是第一个。

诀窍是用另一个引号字符引用每个字符串:

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

Ben Lee对正则表达式的看法是正确的。然而,我仍然认为您希望用"(一个双引号)代替它,而不是''(两个单引号)。

您实际上是在尝试替换('),而不仅仅是单引号字符。在由双引号分隔的字符串中,不需要转义单引号,因为它们并不表示字符串的结束。。。

替换将只替换匹配的第一个引号,除非您使用的是我们的老朋友正则表达式。缺点是正则表达式往往速度较慢。

我还没有检查哪个更快,但你可以尝试使用拆分:

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

只需尝试以下操作:

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

它基本上是用两个单引号字符来更改单引号字符,对吧?如果是这种情况,您可能希望在正则表达式的末尾使用全局标志g,并将其重新分配给您的值(temp):

temp = temp.replace(/'/g,"''");
while (str.indexOf("'") >= 0)
    {
        var newStr = str.replace("'", "'"");
        str = newStr;
    }