简体   繁体   English

正则表达式?

[英]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. 我想从中获取文本( "Hello" )。

I've done that successfully with following code, but .textContent is not working in IE 我已经成功完成以下代码,但是.textContent在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. 我认为您使用jQuery框架。 i think yo should use .text() or .html() 我认为您应该使用.text().html()

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

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

good luck 祝好运

Getting the hello? * 得到你hello? * hello? * part instead of the whole 1 hello? * hello? *部分而非全部1 hello? * 1 hello? * would be much better. 1 hello? *会好得多。 To do this, we need to get the second child node of #questionText: 为此,我们需要获取#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. 剩下的就是大写第一个字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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