用大写字母替换HTML标签中的第一个字母

时间:2016-10-19 18:47:49

标签: javascript jquery string

由于文本的性质,这不能用CSS结构伪类解决。并非所有句子都在新的<p>或任何标记内开始。我正在处理某些符号或标签之后的文字,例如.<br/>。对于大多数阅读完整的问题已经注意到了这一点,但我已经被要求在我的问题的顶部添加这个解释为少数表明已经在CSS结构伪类的形式上已经回答堆栈溢出即p::first-letter

示例字符串是

<h3 class="media-heading">description</h3>
    <p>there is a john entrance hall with staircase, beautiful lounge with stone fireplace and wood-burner, exposed at stone walls and beams. kitchen/ dining room, is huge room with fitted kitchen, wood burner and dining area with doors to the terrace.&nbsp; upstairs there are 3 double bedrooms, the master with en-suite bathroom, family shower room and a landing.there is a self contained apartment, with private terrace, (which could be incorporated into the house,) which has a lounge, bedroom, kitchen and shower room.the cottage/gite has an entrance hall, lounge with fireplace, fitted kitchen, 4 bedrooms, bathroom and shower room. outside there is a&nbsp; private garden and solar shower.&nbsp; rental is very popular in the summer in this region and this gite could be rented out for over £1000 per week.<br _moz_dirty="true"></p><p>paragraph break<br>line break</p>

通缉输出

<h3 class="media-heading">Description</h3>
    <p>There is a john entrance hall with staircase, beautiful lounge with stone fireplace and wood-burner, exposed at stone walls and beams. Kitchen/ dining room, is huge room with fitted kitchen, wood burner and dining area with doors to the terrace. Upstairs there are 3 double bedrooms, the master with en-suite bathroom, family shower room and a landing. There is a self contained apartment, with private terrace, (which could be incorporated into the house,) which has a lounge, bedroom, kitchen and shower room. The cottage/gite has an entrance hall, lounge with fireplace, fitted kitchen, 4 bedrooms, bathroom and shower room. Outside there is a private garden and solar shower. Rental is very popular in the summer in this region and this gite could be rented out for over £1000 per week.<br _moz_dirty="true"></p><p>Paragraph break<br>line break</p>

我正在做的是获取HTML标记大写后的第一个字母以及.之后的第一个字母。基本上使文本人性化。我发现一些很棒的字符串人性化http://stringjs.com是一个很好的例子,但它不适用于上面的示例字符串。我猜是因为<是第一个角色。

任何帮助将不胜感激。除非我可以用格式化的字符串替换文本,否则剥离HTML不是一个选项。

2 个答案:

答案 0 :(得分:2)

您可以使用.html(function)String.prototype.replace()RegExp /((^|\.)|(\.(?:\s+|&nbsp;(?:\s+))|<br>))[a-z]/g匹配字符串开头的第一个a-z字符或.后跟{ {1}}或a-z后跟.,或a-z后跟.,后跟空格&nbsp;a-z,或{{1然后是a-z; <br>a-z来处理不间断的空格字符String.prototype.slice()String.prototype.indexOf()

&#13;
&#13;
&nbsp;
&#13;
String.prototype.toUpperCase()
&#13;
&#13;
&#13;

答案 1 :(得分:-2)

您可以使用bacis CSS样式 资本化单词的第一个字母

 p {
        text-transform:capitalize;
    }