Javascript 用大写字母替换HTML标记中句子的第一个字母

Javascript 用大写字母替换HTML标记中句子的第一个字母,javascript,jquery,string,Javascript,Jquery,String,由于文本的性质,这不是CSS结构伪类可以解决的问题。并非所有的句子都是在一个新的或任何标记中开始的。我想要的东西也会在某些符号或标记后大写文本,比如和。对于大多数阅读者来说,这个问题已经注意到了这一点,但是我被要求在我的问题的顶部添加这个解释,因为少数人认为这已经以CSS结构伪类的形式在堆栈溢出上得到了回答,即p::first letter 一个示例字符串是 <h3 class="media-heading">description</h3> <p>t

由于文本的性质,这不是CSS结构伪类可以解决的问题。并非所有的句子都是在一个新的
或任何标记中开始的。我想要的东西也会在某些符号或标记后大写文本,比如

。对于大多数阅读者来说,这个问题已经注意到了这一点,但是我被要求在我的问题的顶部添加这个解释,因为少数人认为这已经以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>
说明
有一个带楼梯的约翰入口大厅,有石壁炉和木燃烧器的美丽休息室,暴露在石墙和横梁上。厨房/餐厅,是一个巨大的房间,配有厨房、木燃烧器和餐厅,有通往阳台的门。楼上有3间双人卧室,主卧带套房浴室、家庭淋浴间和楼梯平台。有一套独立的公寓,带有私人露台(可并入住宅),其中有休息室、卧室、厨房和淋浴间。小屋/吉特有入口大厅、带壁炉的休息室、配备厨房、,4间卧室、浴室和淋浴间。外面有一个私人花园和太阳能淋浴。夏季租赁在该地区非常流行,这台gite可以以每周1000英镑以上的价格出租。段落中断
换行符

想要的产出

<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>
说明
有一个带楼梯的约翰入口大厅,有石壁炉和木燃烧器的美丽休息室,暴露在石墙和横梁上。厨房/餐厅,是一个巨大的房间,配有厨房、木燃烧器和餐厅,有通往阳台的门。楼上有3间双人卧室,主卧配有套房浴室、家庭淋浴间和一个平台。有一个独立的公寓,带有私人露台(可以并入房子),有休息室、卧室、厨房和淋浴间。小屋/吉特有入口大厅、带壁炉的休息室、配备厨房、4间卧室、浴室和淋浴间。外面有一个私人花园和太阳能淋浴。夏季租赁在该地区非常流行,这台gite可以以每周1000英镑以上的价格出租。段落中断
换行符


我所要做的是将HTML标记后面的第一个字母大写,以及
后面的第一个字母大写。基本上使文本人性化。我发现许多优秀的字符串人性化是一个完美的例子,但它不适用于上面的示例字符串。我猜由于
的原因,您可以使用bacis CSS样式
将单词的第一个字母大写

 p {
        text-transform:capitalize;
    }
您可以使用
.html(function)
String.prototype.replace()
替换为
RegExp
/(^ |\)(\.(?:\s+)(?:\s+)
)[a-z]/g
以匹配字符串开头的第一个
a-z
字符或
后接
a-z
,或
后接
后接空格
a-z
a-z
,或

后接
a-z
String.prototype.slice()
String.prototype.indexOf()
处理不间断空格字符
String.prototype.toUpperCase()

var re=/((^ |\)(\.(?:\s+)(?:\s+))[a-z]/g;
$(“h3,p”).html(函数(3;,html){
返回html.replace(re),函数(match){
var space=“”;
返回match.indexOf(空格)>-1
?match.slice(0,match.indexOf(“;”))
+match.slice(match.indexOf(“;”)+1.toUpperCase()
:match.toUpperCase();
})
})

描述

有一个带楼梯的约翰入口大厅,有石壁炉和木燃烧器的美丽休息室,暴露在石墙和横梁上。厨房/餐厅,是一个巨大的房间,配有厨房、木燃烧器和餐厅,有通往阳台的门。楼上有3间双人卧室,主卧带套房浴室、家庭淋浴间和楼梯平台。有一套独立的公寓,带有私人露台(可并入住宅),其中有休息室、卧室、厨房和淋浴间。小屋/吉特有入口大厅、带壁炉的休息室、配备厨房、,4间卧室、浴室和淋浴间。外面有一个私人花园和太阳能淋浴。在这个地区,夏季租车非常流行,这台gite可以以每周1000英镑以上的价格租出。段落中断
换行

你(可能)不需要使用JavaScript;在大多数情况下,您可以简单地使用CSS:
:第一个字母{text transform:uppercase;}
@DavidThomas可以
k
at
。厨房/
是否在
css中匹配
?链接问题中的任何方法是否匹配
k
中的@War10ck可能重复。厨房
?答案的最后一部分不正确<代码>首字母
不大写句子的第一个字母。有关更多详细信息,请参阅。与此链接混淆:查看
none
initial
的值之间的差异。没有,因为
initial
只是指浏览器应用开箱即用样式表时应用于元素的初始值。在这种情况下,
text transform
的初始状态是
none
@Vijai在
处不匹配
k
。kitchen
?不幸地触及了问题的表面。是标记编辑器删除了换行符还是上面的代码?@War10ck代码。见最新帖子;用
.text()
@War10ck替换
.html()
,见更新帖子。包括