[英]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,'') ;
如何更改代码,使其在两种环境中都能正常工作?
得到你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.