在句子中如何使首字母UPPERCASE后跟点('。')和空格(零或多于一)

时间:2015-01-07 06:08:45

标签: javascript regex



var t = ".i am krishna.   france" // string

  // regular expression to match the pattern where $1 $2 $3 are the grouping
t = t.replace(/[\n]*([a-zA-Z]+[.][ \n]*)([a-zA-Z])([a-zA-Z]*)[\n]*/, '$1' + '$2'.toUpperCase() + '$3');
 $('body').append(t);

//to convert
function convert() {
	return arguments[0].toUpperCase();
}

<html>
  <head>
    <title></title>
  </head>
  <body></body>
</html>
&#13;
&#13;
&#13;

预期结果:

.I am krishna.  France

2 个答案:

答案 0 :(得分:2)

([.]\s*)([a-zA-Z])

您可以改用'$1'+'$2'.toUpperCase()替换它,以达到您想要的效果。

参见演示。

https://regex101.com/r/sH8aR8/4

答案 1 :(得分:2)

您可以使用:

t = t.replace(/(\. *)([a-z])/g, function($0, $1, $2) { return $1 + $2.toUpperCase(); });
//=> .I am krishna.   France