正则表达式以匹配空格或字符串结尾 [英] Regex to Match White Space or End of String
本文介绍了正则表达式以匹配空格或字符串结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在注释文本中找到@username的每个实例,并将其替换为链接.到目前为止,这是我的PHP:
I'm trying to find every instance of @username in comment text and replace it with a link. Here's my PHP so far:
$comment = preg_replace('/@(.+?)\s/', '<a href="/users/${1}/">@${1}</a> ', $comment);
唯一的问题是正则表达式取决于@username引用后是否有空格.谁能帮助我进行调整,以便它在字符串末尾也能匹配?
The only problem is the regex is dependent upon there being whitespace after the @username reference. Can anyone help me tweak this so it will also match if it is at the end of the string?
推荐答案
尝试使用/@(\w+)/
而不是/@(.+?)\s/
.
这篇关于正则表达式以匹配空格或字符串结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文