如果此字母为小写,则Javascript正则表达式匹配点和空格/空格后的第一个字母

时间:2017-12-12 10:04:11

标签: javascript regex

我想写一个JavaScript正则表达式,它匹配点后面的所有第一个字母,一个或多个空格(换行符),如果这个字母是小写的。我强调了我想要匹配的字母。

enter image description here

图片中的文字:

RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.

Edit the Expression & Text to see matches. roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported.

The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns.

explore results with the Tools below. Replace & List output custom results. Details lists capture groups.explain describes your expression in plain English.

2 个答案:

答案 0 :(得分:1)

你需要这样的东西:

var str = document.getElementById('text').innerHTML;
console.log(str.match(/[.]\s+[a-z]/g));

/*
 EDIT - If you want to get only the letters
*/
var matches = [];
var regex = /[.]\s+([a-z])/g;
while (res = regex.exec(str)) {
  matches.push(res[1]);
}
console.log(matches);
#text {
  width: 100%;
}
<textarea id="text" rows="10">
RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.

Edit the Expression & Text to see matches. roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported.

The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns.

explore results with the Tools below. Replace & List output custom results. Details lists capture groups.explain describes your expression in plain English.
</textarea>

/[.]\s+[a-z]/g

  • [.] - 匹配点字符
  • \s - 匹配单个空格字符,包括空格,制表符,换页符,换行符。相当于[\ f \ n \ r \ t \ v \ u00a0 \ u1680 \ u180e \ u2000- \ u200a \ u2028 \ u2029 \ u202f \ u205f \ u3000 \ ufeff]
  • + - 将前面的表达式匹配1次或更多次。相当于{1,}
  • [a-z] - 匹配az
  • 中的任何小写字母
  • g - 全球搜索

Read more

答案 1 :(得分:0)

我认为这样做:

/\.\s+([a-z]{1})/g

\.匹配一个点

\s+匹配1个或更多空格

([a-z]{1})捕获第一个小写字母。

g全球研究

你可以在这句话中试试:https://regex101.com/r/d6JXSR/1