我正在寻找一个正则表达式来匹配整个字符串中特定单词的开头。假设我有这个:
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]
表示从 a
到 z
的字符类别。 *
表示出现 0 次或多次。
关于javascript - 用于匹配字符串中特定单词的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21086073/