提及用户在文本中计数

时间:2019-04-15 14:17:12

标签: javascript regex

在这里,我有一个@mention用户的字符串,但我不想计算任何带有空格,点,逗号,连字符的用户。

var str= `Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. 
@portpofili.9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393
@portpofili-9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393`;
var pattern = /@\b[-?(\w+)?]+\b/gi;
var count = (str.match(pattern) || []).length;
console.log(count);

这里的结果是10,但我需要8。我该怎么做?在此先感谢

2 个答案:

答案 0 :(得分:5)

您的正则表达式需要修改:

@ [^。\ s-] +(?= \ s | $)

@,后跟不是。,-或空格的任何字符,直到下一个字符是空格或输入的结尾。

var str= `Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. 
@portpofili.9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393
@portpofili-9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393`;
var pattern = /@[^.\s-]+(?=\s|$)/gi;
var count = (str.match(pattern) || []).length;
console.log(count);

答案 1 :(得分:1)

您的模式[-?(\w+)?]由与所有列出的字符匹配的字符类组成(也可以写为[-+?()\w]

您可能要做的是匹配1个以上的字符,并使用否定的\S断言右边直接的字符不是非空格字符(?!

@\w+(?!\S)

Regex demo

var str= `Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. 
@portpofili.9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393
@portpofili-9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393`;
var pattern = /@\w+(?!\S)/gi;
var count = (str.match(pattern) || []).length;
console.log(count)

如果前面只是字符串的开头或空格,则可以使用替代方式将其匹配,并在组中捕获@mention:

(?:^|\s)(@\w+)(?!\S)

var str = `Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
@portpofili.9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393
@portpofili-9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393 @portpofili.9393@portpofili9394 @@test`;
var pattern = /(?:^|\s)(@\w+)(?!\S)/gi;
var count = (str.match(pattern) || []).length;
console.log(count);

从组中获取值的示例:

var pattern = /(?:^|\s)(@\w+)(?!\S)/gi;
const str = `@test
@portpofili.9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393
@portpofili-9393 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393

@portpofili.9393-j @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393
@portpofili-9393-8 @portpofili9394 @portpofili9395 @portpofili9393 @portpofili9393

@portpofili.9393@portpofili9394
@@test

@testing`;
let m;
while ((m = pattern.exec(str)) !== null) {
  if (m.index === pattern.lastIndex) {
    pattern.lastIndex++;
  }
  console.log(m[1]);
}