简体   繁体   English

正则表达式不包括空格

[英]Regex is not including spaces

I'm using some atwho.js to allow linking to users profile using @.我正在使用一些 atwho.js 来允许使用 @ 链接到用户个人资料。

For example @User1 @User2 @User3, and all these would link to their profile.例如@User1 @User2 @User3,所有这些都会链接到他们的个人资料。

I'm getting correct feedback from regexr.com, but it isn't translating,我从 regexr.com 得到正确的反馈,但它没有翻译,

and it's not including the space它不包括空间

Here is what my record displays in the database displays:这是我的记录在数据库中显示的内容:

<p><a href=/profiles/Richard Skiles>@Richard Skiles</a></p>

Here is what the output is:这是 output 是什么:

<p><a href=/profiles/Richard>@Richard Skiles</a></p>

Here is some code:这是一些代码:

public function setBodyAttribute($body)
{
    $find = ['/@([\w\- ]+)/'];
    $replace = ['<a href=/profiles/$1>$0</a>'];
    $this->attributes['body'] = preg_replace($find, $replace, $body);
}

Hopefully someone can help me here.希望有人可以在这里帮助我。 It would be much appreciated, thank you!!将不胜感激,谢谢!!

I saw you added a space in the character set, but the preg_replace doesn't match spaces like that.我看到你在字符集中添加了一个空格,但是preg_replace不匹配这样的空格。 They use the \s class他们使用\s class

public function setBodyAttribute($body)
{
    $find = ['/@([\w\-\s]+)/']; // I added the \s to match the spaces
    $replace = ['<a href=/profiles/$1>$0</a>'];
    $this->attributes['body'] = preg_replace($find, $replace, $body);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM