简体   繁体   中英

regular expression?

I have a tag like below:

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

I want to get the text( "Hello" ) from that.

I've done that successfully with following code, but .textContent is not working in IE

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

How can I change the code, so that it works in both?

i think you ses jquery framework. i think yo should use .text() or .html()

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

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

good luck

Getting the hello? * hello? * part instead of the whole 1 hello? * 1 hello? * would be much better. To do this, we need to get the second child node of #questionText:

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

Now we can filter the string for unwanted characters like this,

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

All that's left is to capitalize the first letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM