簡體   English   中英

正則表達式?

[英]regular expression?

我有一個如下標簽:

<p id="questionText"><span class="qn" id="qn"> 1 </span>  hello? * </p> 

我想從中獲取文本( "Hello" )。

我已經成功完成以下代碼,但是.textContent在IE中無法正常工作

 questionText = $('questionText-').textContent;
 questionText = questionText.replace(/\d+/g,"");
 questionText = questionText.replace("*","");
 return questionText.replace(/^\s*|\s*$/g,'') ;

如何更改代碼,使其在兩種環境中都能正常工作?

我認為您使用jQuery框架。 我認為您應該使用.text().html()

http://api.jquery.com/text/

http://api.jquery.com/html/

祝好運

得到你hello? * hello? *部分而非全部1 hello? * 1 hello? *會好得多。 為此,我們需要獲取#questionText的第二個子節點:

var qt = $("#questionText")[0];
qt.normalize();
var hello = qt.childNodes[1].data;
alert(hello); // outputs "  hello? * "

現在,我們可以像這樣過濾字符串中不需要的字符,

hello = hello.
            replace(/[^a-zA-Z ]/g, "").
            trim();
alert(hello); // outputs "hello"

剩下的就是大寫第一個字母。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM