-2

Consider the following: this.state.message.match(/^\/m\s+(\w+):\s*(.*)/)

This looks for a message that contains: /m name: message where its failing if your name has a space in it: /m name something: message. What would be the proper way to update this such that the name can have spaces or other characters other then just name

for example: \m Character name: message fails, but \m Sample: message does not.

thoughts?

TheWebs
  • 12,470
  • 30
  • 107
  • 211

3 Answers3

0

Simply add space to the character list, no?

/^\/m\s+(\w+[\w| ]*):\s*(.*)/

Change \w to [A-Za-zÀ-ÖØ-öø-ÿ] to accept also accents and diacritics Link

Thanh Trung
  • 3,566
  • 3
  • 31
  • 42
0

Repeat \w+\s+(word characters, followed by spaces) zero or more times:

^\\m\s+((?:\w+\s+)*\w+):\s*(.*)

https://regex101.com/r/RDl628/1

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Try this:

/^\/m\s+(\w+\s+)*?(\w+):\s*(.*)/
thb
  • 13,796
  • 3
  • 40
  • 68
Ilan
  • 624
  • 6
  • 11