javascript - 用于匹配字符串中特定单词的正则表达式

标签 javascript regex

我正在寻找一个正则表达式来匹配整个字符串中特定单词的开头。假设我有这个:

This is the example string of text.

我希望匹配每个以单词开头的 T:

This is the example string of text.

任何帮助将不胜感激。

最佳答案

尝试使用 irb(Ruby):

irb(main):001:0> "This is the example string of text.".scan(/\bt\w+/i)
=> ["This", "the", "text"]

对于/\bt\w+/i\b是边界,t是字符t > 是您想要的开头,\w+ 是字母数字或下划线,出现 1 次或多次。 i 忽略大小写。

如果您只想要字母并且只想匹配t,那么您可以使用

irb(main):002:0> "This is the example string of text.".scan(/\bt[a-z]*/i)
=> ["This", "the", "text"]

[a-z] 表示从 az 的字符类别。 * 表示出现 0 次或多次。

关于javascript - 用于匹配字符串中特定单词的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21086073/

相关文章:

javascript - 隐藏/显示多个 div

php - 匹配两个标签之间所有内容的正则表达式

Python 正则表达式 groups() 和 group() 之间的输出不一致

javascript - nodejs中的setInterval奇怪行为

javascript - `throw ' foo '`, ` throw Error ('foo' )`, ` throw new Error ('foo' )` 有什么区别?

regex - Excel公式中的正则表达式

python - RegEx 获取两个字符串之间有换行符的字符串

python 正则表达式错误 : unbalanced parenthesis

javascript - 网格中的随机 Logo

Javascript - Plotly.js 数字频率顺序