[英]How to get first text node of a string while containing bold and italic tags?
if string(s)'s first part that is: 如果字符串的第一部分是:
" login<b>user</b>account
" “ login<b>user</b>account
”
is enclosed in some element like this : 被包含在这样的一些元素中:
" <div>login<b>user</b>account</div>
", “ <div>login<b>user</b>account</div>
”,
then I can get it with this: 那么我可以这样:
alert($(s).find('*').andSelf().not('b,i').not(':empty').first().html());
// result is : login<b>user</b>account
But how can i get the same result in this condition when it is not enclosed in any element .ie when it is not enclosed in any element? 但是,当它不包含在任何元素中时,即当它不包含在任何元素中时,如何在这种情况下获得相同的结果呢?
I tried this below code which works fine when first part do not include any <b></b>
but it only gives "login" when it does include these tags. 我尝试了下面的代码,当第一部分不包含任何<b></b>
,它可以正常工作,但是当它包含这些标记时,它仅给出“登录” 。
var s = $.trim('login<b>user</b> account<tbody> <tr> <td class="translated">Lorem ipsum dummy text</td></tr><tr><td class="translated">This is a new paragraph</td></tr><tr><td class="translated"><b>Email</b></td></tr><tr><td><i>This is yet another text</i></td> </tr></tbody>');
if(s.substring(0, s.indexOf('<')) != ''){
alert(s.substring(0, s.indexOf('<')));
}
Note: Suggest a generic solution that is not specific for this above string only. 注意:建议一个通用解决方案,该解决方案不只针对上述字符串。 It should work for both the cases when there is bold tags and when there ain't any. 对于有粗体标记和没有粗体标记的情况,它都应该起作用。
So it's just a b
or a i
, heh? 所以它只是一个b
或一个i
,是吗?
A recursive function is always the way to go. 递归函数始终是必经之路。 And this time, it's probably the best way to go. 这次,这可能是最好的方法。
var s = function getEm(elem) {
var ret = ''
// TextNode? Great!
if (elem.nodeType === 3) {
ret += elem.nodeValue;
}
else if (elem.nodeType === 1 &&
(elem.nodeName === 'B' || elem.nodeName === 'I')) {
// Element? And it's a B or an I? Get his kids!
ret += getEm(elem.firstChild);
}
// Ain't nobody got time fo' empty stuff.
if (elem.nextSibling) {
ret += getEm(elem.nextSibling);
}
return ret;
}(elem);
Jsfiddle demonstrating this: http://jsfiddle.net/Ralt/TZKsP/ Jsfiddle对此进行了演示: http : //jsfiddle.net/Ralt/TZKsP/
PS: Parsing HTML with regex or custom tokenizer is bad and shouldn't be done. PS:使用正则表达式或自定义标记生成器解析HTML很不好,因此不应该这样做。
You're trying to retrieve all of the text up to the first element that's not a <b>
or <i>
, but this text could be wrapped in an element itself. 您正在尝试检索直到不是<b>
或<i>
的第一个元素的所有文本,但是此文本可以包装在元素本身中。 This is SUPER tricky. 这是超级棘手的问题。 I feel like there's a better way to implement whatever it is you're trying to accomplish, but here's a solution that works. 我觉得有一种更好的方法来实现您要完成的任务,但这是一种有效的解决方案。
function initialText(s){
var test = s.match(/(<.+?>)?.*?<(?!(b|\/|i))/);
var match = test[0];
var prefixed_element = test[1];
// if the string was prefixed with an element tag
// remove it (ie '<div> blah blah blah')
if(prefixed_element) match = match.slice(prefixed_element.length);
// remove the matching < and return the string
return match.slice(0,-1);
}
You're lucky I found this problem interesting and challenging because, again, this is ridiculous. 您很幸运,我发现这个问题很有趣且具有挑战性,因为这又是荒谬的。
You're welcome ;-) 别客气 ;-)
Try this: 尝试这个:
if (s.substring(0, s.indexOf('<')) != '') {
alert(s.substring(0, s.indexOf('<tbody>')));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.