检测名称和多个提及中的空格

时间:2018-03-26 23:50:46

标签: javascript jquery node.js meteor

我在javascript中创建了一个小提及脚本,在数据库中搜索所提到的用户(在@之后)。

content = content.replace(/@(\w+)?/g, function(match, contents, offset, content) {
    if(Meteor.users.find({username: contents}).count() > 0) {
        return "<a href=''>@"+Meteor.users.findOne({username: contents}).username+"</a>";
    } else {
        return contents;
    }
});

这里有两个问题:

  1. 如果名称包含空格(@admin无效但@this is a user不起作用),则无效。
  2. 由于流星必须在数据库中搜索每个可能的结果,因此效率非常低。
  3. 是否可以从提及中删除所有空格并仍然成功搜索用户?如果是这样,怎么样? 喜欢... @MyUsername,但真实用户名为My Username

0 个答案:

没有答案