/PM 用于在聊天室发送消息的正则表达式语法

发布于 2024-08-11 07:34:49 字数 466 浏览 11 评论 0原文

我正在开发一个 AJAX/PHP 聊天室,目前一直在使用正则表达式来检测用户是否发送了 PM & 消息。然后弄清楚它是谁以及消息是什么。

如果用户输入类似的内容

/pm PezCuckow 嗨,你太棒了!

我想首先测试我的字符串是否与该模式匹配,然后获取“PezCuckow”和“嗨,你太棒了!”作为要发布到 PHP 的字符串。

我对正则表达式做了一些研究,但真的不知道从哪里开始! 你能帮忙吗?

==感谢大家的帮助,现在已经解决了!==

var reg = /^\/pm\s+(\w+)\s+(.*)$/i;
var to = "";

if(message.match(reg)) {
    m = message.match(reg);
    to = m[1];
    message = m[2];
}

I am working on a AJAX/PHP chatroom and am currently stuck on the regex to detect if a user has send a PM & then work out who it is too and what the message is.

If the user types something like

/pm PezCuckow Hi There you so awesome!

I would like to first test if my string matched that pattern then get 'PezCuckow' and 'Hi There you so awesome!' as strings to post to the PHP.

I have done some research on regex but really have no idea where to start with this one!
Can you help?

==Thanks to everyones help this is now solved!==

var reg = /^\/pm\s+(\w+)\s+(.*)$/i;
var to = "";

if(message.match(reg)) {
    m = message.match(reg);
    to = m[1];
    message = m[2];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆° 2024-08-18 07:34:50

假设名称字段中只有单词字符(无空格等)有效,这将执行您想要的操作:

var re = /(\/\w+) (\w+) (.+)/;

Assuming that only word characters (no spaces, etc) are valid in the name field, this'll do what you want:

var re = /(\/\w+) (\w+) (.+)/;
七堇年 2024-08-18 07:34:49

此正则表达式解析消息:

^(?:\s*/(\w+)\s*(\w*)\s*)?((?:.|[\r\n])*)$

说明:

^              # start-of-string
(?:            # start of non-capturing group
  \s*/         #   a "/", preceding whitespace allowed
  (\w+)        #   match group 1: any word character, at least once (e.g. option)
  \s+          #   delimiting white space
  (\w*)        #   match group 2: any word character (e.g. target user)
  \s+          #   delimiting white space
)?             # make the whole thing optional
(              # match group 3:
  (?:          #   start of non-capturing group, either
    .          #     any character (does not include newlines)
    |          #     or
    [\r\n]     #     newline charaters
  )*           #   repeat as often as possible
)              # end match group 3

在您的情况下 ("/pm PezCuckow Hi There you so Awesome!"):

  • group 1: "pm"
  • group 2: "PezCuckow"
  • group 3: "Hi There你太棒了!”

在更一般的情况下(“嗨,你太棒了!”

  • 第 1 组:“”
  • 第 2 组:“”
  • 第 3 组:“嗨,你太棒了!”

请注意,正斜杠需要在 JavaScript 正则表达式文字中转义:

/foo\/bar/

但通常在正则表达式模式中不需要转义。

This regex parses a message:

^(?:\s*/(\w+)\s*(\w*)\s*)?((?:.|[\r\n])*)$

Explanation:

^              # start-of-string
(?:            # start of non-capturing group
  \s*/         #   a "/", preceding whitespace allowed
  (\w+)        #   match group 1: any word character, at least once (e.g. option)
  \s+          #   delimiting white space
  (\w*)        #   match group 2: any word character (e.g. target user)
  \s+          #   delimiting white space
)?             # make the whole thing optional
(              # match group 3:
  (?:          #   start of non-capturing group, either
    .          #     any character (does not include newlines)
    |          #     or
    [\r\n]     #     newline charaters
  )*           #   repeat as often as possible
)              # end match group 3

In your case ("/pm PezCuckow Hi There you so awesome!"):

  • group 1: "pm"
  • group 2: "PezCuckow"
  • group 3: "Hi There you so awesome!"

in a more general case ("Hi There you so awesome!")

  • group 1: ""
  • group 2: ""
  • group 3: "Hi There you so awesome!"

Note that the forward slash needs to be escaped in JavaScript regex literals:

/foo\/bar/

but not in regex patterns in general.

甜中书 2024-08-18 07:34:49

怎么样:

var reg = /^\/pm\s+(\w+)\s+(.*)$/i,
    m = '/pm PezCuckow Hi There you so awesome!'.match(reg);

m[0]; // "PezCuckow"
m[1]; // "Hi There you so awesome!"

匹配 "/pm" 后跟空格 " " (随意接受额外的空格),后跟用户名 \w+,接下来是空格 " " agin,最后是消息 .* (基本上是到行尾的所有内容)。

Hows about this:

var reg = /^\/pm\s+(\w+)\s+(.*)$/i,
    m = '/pm PezCuckow Hi There you so awesome!'.match(reg);

m[0]; // "PezCuckow"
m[1]; // "Hi There you so awesome!"

That matches "/pm" followed by whitespace " " (liberally accepting extra spaces), followed by the username \w+, followed by whitespace " " agin, then finally the message .* (which is basically everything to the end of the line).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文