javascript
  • jquery
  • regex
  • 2016-09-06 381 views 1 likes 
    1

    我有這樣的字符串。如何從字符串中刪除斜線(\)

    ""\"\\\"\\\\\\\"<img title='In Progress' src='img/In Progress.ico' class='clsstatusimginner'/><span class='clstaskidlink' title='Explore about Master Setting in TP' onclick='taskLink(13721,89,Explore about Master Setting in TP)'>Explore about Master Setting in TP</span>\\\\\\\"\\\"\""" 
    

    我試圖像下面的方式從字符串中提取圖像標記。 如何刪除額外的斜槓。

    <img title='In Progress' src='img/In Progress.ico' class='clsstatusimginner'/><span class='clstaskidlink' title='Explore about Master Setting in TP' onclick='taskLink(13721,89,Explore about Master Setting in TP)'>Explore about Master Setting in TP</span> 
    

    我需要從字符串中刪除斜槓(「\」),請任何人都可以幫助我。 謝謝。

    更新1:我得到了這個原因的起源。 但我不明白爲什麼會發生這種情況。 問題是:

    var vrTaskSubject= $("#lblTskSubject").text() 
    

    上方的一個賦予的結果是這樣的:

    ""\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"<img title='Planned' src='img/Planned.ico' class='clsstatusimginner'/><span class='clstaskidlink' title='Format the Campus drive machine and set up the machine ready for campus' onclick='taskLink(14492,133,Format the Campus drive machine and set up the machine ready for campus,)'>Format the Campus drive machine and set up the machine ready for campus</span>\\\\\\\\\\\\\\\"\\\\\\\"\\\"\""" 
    

    然後只要這是字符串化它增加更多的斜線這樣

    vrTskSubject = JSON.stringify($("#lblTskSubject").text()); 
    

    這是給結果是這樣的。

    ""\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"<img title='Planned' src='img/Planned.ico' class='clsstatusimginner'/><span class='clstaskidlink' title='Format the Campus drive machine and set up the machine ready for campus' onclick='taskLink(14492,133,Format the Campus drive machine and set up the machine ready for campus,)'>Format the Campus drive machine and set up the machine ready for campus</span>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\"\\\"\""" 
    

    像這樣多次字符串被串化,許多斜線添加。 請幫我理解爲什麼這樣做。這背後有沒有理由。 我怎樣才能解決這個問題,請需要幫助

    +3

    什麼是這些多餘的斜線的起源除去反斜槓和額外的報價?我想,這是應該修正的 –

    +0

    現在我不知道它在哪裏。 無論如何,讓我試圖找出原因。 然後我會回來,但是如果你有什麼解決方案的意思,請與我分享 –

    +0

    你能否檢查更新的問題 –

    回答

    0

    試試這個:

    myString.replace(/\\/g, "")

    一個完全工作的例子是:

    var myString = "aa ///\\\\\\"; 
    myString.replace(/\\/g, "") 
    // Outputs : "aa ///" 
    

    g標誌將刪除的\每一個實例在你的字符串中。我們必須使用\來逃避模式,否則你會得到一個錯誤。

    編輯1:與OP聊天(你可以看到在下面的評論完整的對話)看到後,這裏是HIS具體問題的解決方案:

    首先,刪除吵個字符。從服務器

    taskSubject = sourceGrid[0].TaskName.replace(/#%&%#/g, ""); 
    

    來的字符串中然後,使用

    myString.replace(/\\|"/g, "") 
    
    +0

    /!= \不需要找什麼OP –

    +0

    哦!對不起,這對我來說太早了,謝謝你告訴我。 – MEGADEVOPS

    +0

    您是否檢查過該代碼段,該代碼不適用於我@ MEGADEVOPS –

    相關問題