2011-01-14 966 views
1

我知道我錯過了一些簡單的東西,但無法弄清楚,所以我會問專家。解碼Drupal中的十六進制js

在Drupal的通過jQuery和hook_menu和Ajax我結束了這個字符串:

\x3cform action=\"/comment/reply/1360\" accept-charset=\"UTF-8\" method=\"post\" id=\"comment-form\" class=\"ajax-form\"\x3e\n\x3cdiv\x3e\x3cdiv class=\"form-item\"\x3e\n \x3clabel\x3eYour name: \x3c/label\x3e\n \x3ca href=\"https://stackoverflow.com/users/testuser1\" title=\"View user profile.\"\x3etestuser1\x3c/a\x3e\n\x3c/div\x3e\n\x3cdiv class=\"form-item\" id=\"edit-comment-wrapper\"\x3e\n \x3clabel for=\"edit-comment\"\x3eComment: \x3cspan class=\"form-required\" title=\"This field is required.\"\x3e*\x3c/span\x3e\x3c/label\x3e\n \x3ctextarea cols=\"60\" rows=\"15\" name=\"comment\" id=\"edit-comment\" class=\"form-textarea required\"\x3e\x3c/textarea\x3e\n \x3cdiv class=\"description\"\x3eComments are limited to a maximum of \x3cem\x3e200\x3c/em\x3e characters.\x3c/div\x3e\n\x3c/div\x3e\n\x3cul class=\"tips\"\x3e\x3cli\x3eWeb page addresses and e-mail addresses turn into links automatically.\x3c/li\x3e\x3cli\x3eAllowed HTML tags: \x26lt;a\x26gt; \x26lt;em\x26gt; \x26lt;strong\x26gt; \x26lt;cite\x26gt; \x26lt;code\x26gt; \x26lt;ul\x26gt; \x26lt;ol\x26gt; \x26lt;li\x26gt; \x26lt;dl\x26gt; \x26lt;dt\x26gt; \x26lt;dd\x26gt;\x3c/li\x3e\x3cli\x3eLines and paragraphs break automatically.\x3c/li\x3e\x3c/ul\x3e\x3cp\x3e\x3ca href=\"/filter/tips\"\x3eMore information about formatting options\x3c/a\x3e\x3c/p\x3e\x3cinput type=\"hidden\" name=\"form_build_id\" id=\"form-0c4051b953791d21147f76e11c2ab7c4\" value=\"form-0c4051b953791d21147f76e11c2ab7c4\" /\x3e\n\x3cinput type=\"hidden\" name=\"form_token\" id=\"edit-form-token\" value=\"8dd5324de6e086d9412e9407eb7aba1e\" /\x3e\n\x3cinput type=\"hidden\" name=\"form_id\" id=\"edit-comment-form\" value=\"comment_form\" /\x3e\n\x3cinput type=\"submit\" name=\"op\" id=\"edit-submit\" value=\"COMMENT\" class=\"form-submit ajax-trigger\" /\x3e\n\x3cinput type=\"submit\" name=\"op\" id=\"edit-preview\" value=\"Preview\" class=\"form-submit ajax-trigger\" /\x3e\n\n\x3c/div\x3e\x3c/form\x3e\n</div> 

,我需要它結束了,因爲這時候它被寫入到div:

<form action="/comment/reply/1360" accept-charset="UTF-8" method="post" id="comment-form" class="ajax-form"> <div><div class="form-item"> <label>Your name: </label> <a href="https://stackoverflow.com/users/testuser1" title="View user profile.">testuser1</a> </div> <div class="form-item" id="edit-comment-wrapper"> <label for="edit-comment">Comment: <span class="form-required" title="This field is required.">*</span></label> <textarea cols="60" rows="15" name="comment" id="edit-comment" class="form-textarea required"></textarea> <div class="description">Comments are limited to a maximum of <em>200</em> characters.</div> </div> <ul class="tips"><li>Web page addresses and e-mail addresses turn into links automatically.</li><li>Allowed HTML tags: &lt;a&gt; &lt;em&gt; &lt;strong&gt; &lt;cite&gt; &lt;code&gt; &lt;ul&gt; &lt;ol&gt; &lt;li&gt; &lt;dl&gt; &lt;dt&gt; &lt;dd&gt;</li><li>Lines and paragraphs break automatically.</li></ul><p><a href="/filter/tips">More information about formatting options</a></p><input type="hidden" name="form_build_id" id="form-0c4051b953791d21147f76e11c2ab7c4" value="form-0c4051b953791d21147f76e11c2ab7c4" /> <input type="hidden" name="form_token" id="edit-form-token" value="8dd5324de6e086d9412e9407eb7aba1e" /> <input type="hidden" name="form_id" id="edit-comment-form" value="comment_form" /> <input type="submit" name="op" id="edit-submit" value="COMMENT" class="form-submit ajax-trigger" /> <input type="submit" name="op" id="edit-preview" value="Preview" class="form-submit ajax-trigger" /> </div></form> 

所以我寫了這個:

function unjsEncode(string){ 

var leftCaret = /\x3c/ig; 
var rightCaret = /\x3e/ig; 
var lineBreak = /\n/ig; 
var escQuote = /\"/ig; 

var newstring = string.replace(leftCaret, "<"); 
    newstring = newstring.replace(rightCaret, ">"); 
    newstring = newstring.replace(lineBreak, " "); 
    newstring = newstring.replace(escQuote, '"'); 
    return newstring; 

}

但它不起作用。這是漫長的研究如何在Drupal中編寫動態表單的結尾,我覺得自己像個白癡,我無法在js中找出一個簡單的正則表達式例程,但是在8小時之後,我就是在這裏。任何幫助教我如何釣魚將不勝感激!

+0

想通了: – vaene 2011-01-14 07:24:42

回答

0

根據您的編程風格,你可能還需要添加以下行來處理單引號:

newstring = newstring.replace(/\\'/g,"'");