The Angry Saxon The Angry Saxon - 1 month ago 7
Javascript Question

Replace first letter of sentence in HTML tags with capital letter

An example string would be

<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>


Wanted Output

<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>


What I'm after doing is getting the first letter after a HTML tag capitalised and also after the
.
. Humanising the text basically. I've found a number of great string humanising http://stringjs.com is a perfect example but it doesn't work for the example string above. I'm guessing due to the
<
being the first character.

Any help would be appreciated. Stripping HTML out isn't an option really unless I can replace the text with the formatted string.

Answer

You can use .html(function), String.prototype.replace() with RegExp /(\.(?:\s+|&nbsp;(?:\s+))|<br>)[a-z]|^[a-z]/g to match first a-z character at beginning of string or . followed by a-z or . followed by a-z, or . followed by &nbsp; followed by space a-z or a-z, or <br> followed by a-z; String.prototype.slice(), String.prototype.indexOf() to handle non-breaking space character &nbsp;, String.prototype.toUpperCase()

var re = /(\.(?:\s+|&nbsp;(?:\s+))|<br>)[a-z]|^[a-z]/g;

$("h3, p").html(function(_, html) {
  return html.replace(re, function(match) {
    var space = "&nbsp;";
    return match.indexOf(space) > -1 
           ? match.slice(0, match.indexOf(";")) 
             + match.slice(match.indexOf(";") + 1)
               .toUpperCase()
           : match.toUpperCase();
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

Comments