简体   繁体   English

使用CSS或jQuery将匹配字符串与HTML标题分开

[英]Separate matching strings from title in HTML using CSS or jQuery

I am working on a project which requires a separation of strings from all the titles listed. 我正在一个项目中,该项目要求将字符串与列出的所有标题分开。 Titles are more than 500 so its difficult to change it. 标题超过500个,因此很难更改。

The list of doctors 医生名单

 <div class="dr-title"><h2>Dr. Nicki Mallmann CRM 22768</h2></div> <div class="dr-title"><h2>Dr. Alexandre Manoel Varela CRM 10113</h2></div> <div class="dr-title"><h2>Dr. Arnaldo Laffitte Stier Junior CRM 9475</h2></div> <div class="dr-title"><h2>Dr. Claudinei Colatusso CRM 19994</h2></div> 

All those are wrapped in HTML within h2 elements. 所有这些都包装在h2元素内的HTML中。

Are there any possibilities that we can separate the texts starting from CRM followed with its unique number? 我们是否有可能将CRM开头的文本及其唯一编号分开?

What currently it looks like is above. 目前看起来像上面。 What I want is below 我想要的是下面

 .dr-crm { display: block; font-size: 12px; } 
 <div class="dr-title"><h2>Dr. Nicki Mallmann <span class="dr-crm">CRM 22768</span></h2></div> <div class="dr-title"><h2>Dr. Alexandre Manoel Varela <span class="dr-crm">CRM 10113</span></h2></div> <div class="dr-title"><h2>Dr. Arnaldo Laffitte Stier Junior <span class="dr-crm">CRM 9475</span></h2></div> <div class="dr-title"><h2>Dr. Claudinei Colatusso <span class="dr-crm">CRM 19994</span></h2></div> 

Is it possible with CSS or jQuery? CSS或jQuery是否可能? I tried looking for something with css pseudo selectors but not possible. 我试图用css伪选择器寻找东西,但不可能。 Might be with jQuery I think its possible. 我认为它可能与jQuery一起使用。

CSS is not powerful enough to amend the content as you require in this case. 在这种情况下,CSS的功能不足以修改内容。

Instead, you can achieve this using Javascript by providing a regular expression to the replace() method which finds the CRM value and any following characters, and wraps them in a span . 取而代之的是,您可以使用Javascript来实现此目的,方法是为replace()方法提供正则表达式,该方法查找CRM值和以下任何字符,并将它们包装在span Try this: 尝试这个:

 $('h2').html(function(i, h) { return h.replace(/(CRM.*$)/, '<span class="dr-crm">$1</span>'); }); 
 .dr-crm { display: block; font-size: 12px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="dr-title"> <h2>Dr. Nicki Mallmann CRM 22768</h2> </div> <div class="dr-title"> <h2>Dr. Alexandre Manoel Varela CRM 10113</h2> </div> <div class="dr-title"> <h2>Dr. Arnaldo Laffitte Stier Junior CRM 9475</h2> </div> <div class="dr-title"> <h2>Dr. Claudinei Colatusso CRM 19994</h2> </div> 

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

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