粗体文本中的部分字符串 [英] Parts of string in bold text

查看:70
本文介绍了粗体文本中的部分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无序列表,其中包含

I have a unordered list that contains

<ul id="strip">
     <li><a href="#"><span>This-is a test string</span></a></li>
    <li><a href="#"><span>This is without</span></a></li>
     <li><a href="#"><span>New-test</span></a></li>
</ul>

我需要在 - 之前加粗文字,所以在第一个< li> 这个是粗体。

I need to bold the text before the "-", so in the first <li> "This" is bolded.

我陷入了循环,我应该找到 - 。

I'm stuck in the loop where I should find the "-".

注意:常规JavaScript,没有JQuery: - )

NB: Regular JavaScript, no JQuery :-)

推荐答案

使用 stringObject.replace(findstring,newstring)方法。

在你的情况下 liString.replace(/ \b(。 *? - 。*?)\ b /,< strong> $ 1< / strong>)

完整解决方案:

Use stringObject.replace(findstring,newstring) method.
In your case liString.replace(/\b(.*?-.*?)\b/,"<strong>$1</strong>")
full solution:

var lists = document.getElementsByTagName("li");
var listsL = lists.length;

for (var i = 0; i < listsL; ++i){
   liString = lists[i].innerHTML;
   liString = liString.replace(/\b([^-]*-[^\b]*?)\b/,"<strong>$1</strong>");
   lists[i].innerHTML = liString;
}

这篇关于粗体文本中的部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆