admin管理员组文章数量:1402786
I have a textarea, where I want every first letter in each sentence to be capitalized.
Now I have this:
evt.target.value = evt.target.value.replace(/.+?[\.\?\!](\s|$)/g, function(a) {
return a.charAt(0).toUpperCase() + a.slice(1);
});
And this goes in af function, where evt.target
is the textarea, and the function is called on keyup.
It works great, except that it doesn't capitalize instantly. If I write:
Hey. i need some regular expression for this.
then the "I" will capitalize only when i put a period at the end of "this". I would like it to capitalize the "I" instantly after typing it, if it es after an ". >whitespace<" or "! >whitespace<" or "? >whitespace<".
I tried googling a lot of stuff, but unfortunately, this regex stuff is too plicated for me. Could anyone write this correctly, with a quick explanation?
I have a textarea, where I want every first letter in each sentence to be capitalized.
Now I have this:
evt.target.value = evt.target.value.replace(/.+?[\.\?\!](\s|$)/g, function(a) {
return a.charAt(0).toUpperCase() + a.slice(1);
});
And this goes in af function, where evt.target
is the textarea, and the function is called on keyup.
It works great, except that it doesn't capitalize instantly. If I write:
Hey. i need some regular expression for this.
then the "I" will capitalize only when i put a period at the end of "this". I would like it to capitalize the "I" instantly after typing it, if it es after an ". >whitespace<" or "! >whitespace<" or "? >whitespace<".
I tried googling a lot of stuff, but unfortunately, this regex stuff is too plicated for me. Could anyone write this correctly, with a quick explanation?
Share Improve this question edited Oct 8, 2016 at 9:32 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Oct 7, 2016 at 13:54 RevToidRevToid 231 silver badge3 bronze badges 2-
2
Maybe
.replace(/([!?.]\s+)([a-z])/g, function(m, $1, $2) {return $1+$2.toUpperCase();})
? – Wiktor Stribiżew Commented Oct 7, 2016 at 13:58 - I remend you www.regex101. for regex testing... – Emilio Grisolía Commented Oct 7, 2016 at 14:00
2 Answers
Reset to default 7I'm late, but maybe somebody needs jQuery code for it. That works for first sentence too.
$('input').on('input', function (evt) {
var $this = $(evt.target),
re = /(^|[.!?]\s+)([a-z])/g,
val = $this.val().replace(re, function (m, $1, $2) {
return $1 + $2.toUpperCase();
});
$this.val(val);
});
https://jsfiddle/chukanov/oqg5o88u/1/
es6 update:
let re = /(^|[.!?]\s+)([a-z])/g;
$('input').on('input', function (evt) {
let $this = $(this),
val = $this.val().replace(re, (m, $1, $2) => $1 + $2.toUpperCase());
$this.val(val);
});
https://jsfiddle/chukanov/oqg5o88u/27/
I suggest using /([!?.]\s+)([a-z])/g
regex and pass the match to the replace callback function to only upper the second capturing group:
let log = document.querySelector('#log'),
test = document.querySelector('#test');
test.addEventListener('input', e => {
log.innerHTML = e.target.value.replace(/([!?.]\s+)([a-z])/g, function(m, $1, $2) {
return $1+$2.toUpperCase();
});
});
<input type='text' id='test' autofocus /><br />
<span id='log'></span>
Pattern details:
([!?.]\s+)
- Group 1 capturing a!
,?
or.
and 1 or more whitespaces (NOTE that replacing+
with*
, zero or more whitespaces will be allowed)([a-z])
- Group 2 capturing lowercase ASCII letters.
本文标签:
版权声明:本文标题:regex - How to capitalize first letter after period, question mark and exclamation in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744351624a2602083.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论