2012-02-29 52 views
-1

我试图使用替换法更换“””‘‘’字符串的字符’,像这样:的Javascript替换““‘和’‘’”

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> 

我必须更换单一到两个单曲我不能更改为“但是”

+2

的好建议在这个问题上不缺,LOL – Zoidberg 2012-02-29 12:28:16

回答

-4
while (str.indexOf("'") >= 0) 
    { 
     var newStr = str.replace("'", "\""); 
     str = newStr; 
    } 
+0

我现在有另一个问题,我试图将这个XML保存在一个mssql中,但得到一个错误消息,可能是什么问题? – user1176926 2012-02-29 12:46:58

+3

为什么这个答案与所有​​其他更好的答案相反?另外,如果要用双引号替换单引号,为什么问题中的原始代码在替换的第二部分中有两个单引号? – Zoidberg 2012-02-29 12:49:32

+0

你的错误信息是什么? – gabitzish 2012-02-29 12:50:55

11

您的代码只替换了一个实例(它找到的第一个实例),您应该替换所有实例,可以使用正则表达式并在末尾添加g标志意思是“全球搜索”。像这样:

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

这是一个工作示例:http://jsfiddle.net/Q2Uyv/(在“In”框中键入内容并单击“Convert”)。

如果你实际上是试图取代单引号用双引号(而不是单引号的一倍序列),这样做:

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

不工作,你确定你写它正确地将? – user1176926 2012-02-29 12:27:54

+0

@ user1176926,它正在工作。例如:http://jsfiddle.net/hmY77/ – 2012-02-29 12:31:21

+0

@ user1176926,我在我的答案中添加了一个不同的工作示例,您可以在其中输入任何字符串以确认其有效。 – 2012-02-29 12:36:01

1

诀窍是引用每个字符串与其他引号字符:

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

编辑:本·李是正确的正则表达式,更新以上。但是,我仍然收集它,你想用"(一个双引号),而不是''(两个单引号)替换。

0

你有没有试过只是这个?

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

这只会取代第一个实例。 – 2012-02-29 12:26:06

2

的问题是,

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

只会取代的”第一个实例。为了解决这个问题,做的却

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

以下这将确保它去,但并取代了单引号,而不是仅仅是第一的所有实例。

0

所以基本上改变与2个单引号单引号字符,对不对?如果是这样的话,你可能会想使用全局标志,g在yoir正则表达式结束assing回你的价值(临时)

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

你实际上是试图取代( '),而不仅仅是单引号字符。没有必要为了逃避双引号分隔字符串中的单引号,因为他们不表示一个字符串的结尾......

替换只会替换第一个报价匹配,除非您使用的是老朋友Regexp。正则表达式的缺点往往是缓慢的。 我没有检查,看看这是更快,但你可以尝试采用分体式:

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

test = "string cont'aining single 'quotes".split("'").join("''");//in firebug console 
//returns "string cont''aining single ''quotes" as expected