I need to capitalize the first letter of each line inside a div
In the example below the result should be:
Lorem ipsum
Dolor sit
Amet
So each n and first letter
should be replaced by n and capital letter
Please help me abut the regex part of my code
$('button').on('click', function(){
let str = $('#wrap').text();
let result = str.replace(/n a-z/, /n A-Z/); // how to write this?
$('#wrap').text(result);
});
.wrap{white-space:pre-wrap;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='wrap' id='wrap'>
lorem ipsum
dolor sit
amet
</div>
<button>CLICK</button>
2
Answers
Consider the following.
This assumes that your HTML or Text has End of Line characters (
rn
or justn
).You can use
ns?p{Ll}
.And use the replacement function to match the first letter and replace it with its upper case with
toUpperCase()
function.