如何获得以这句话开头的正则表达式

How do I get a regular expression that starts with this phrase?

本文关键字:正则表达式 开头 这句话 何获得      更新时间:2023-09-26

我想要一个正则表达式,它接受以"st"开头的较大字符串中的短语。它必须不区分大小写,并搜索整个字符串并返回匹配数。

我有什么:

/st+/ig

我正在测试它:

st stuff pistuff stuff
stuff st st st st stuff notstuff
STUFF STUFF STUFF
sti
stiiiiii
stiiii
stufff
stiufff

它几乎可以正确匹配所有内容,除了它也拾取 pistuff 和 notstuff。

更新以捕获以 st 开头的单词或仅捕获两个字符st,不区分大小写:

  • 对于整个单词匹配:/'bst'w*/ig
  • 对于匹配,无需匹配整个单词:/'bst/ig

'b用于标记单词边界。

var strArr = [
  'st stuff pistuff stuff',
  'stuff st st st st stuff notstuff',
  'STUFF STUFF STUFF',
  'sti',
  'stiiiiii',
  'stiiii',
  'stufff',
  'stiufff'
];
var re = /'bst'w*/ig;
var count = 0;
strArr.forEach(function(str) {
  count += str.match(re).length;
  document.body.insertAdjacentHTML('beforeend', str.match(re) + '<br>');
});
document.body.insertAdjacentHTML('beforeend', 'count: ' + count);

此外,正则表达式101