Home > Back-end >  RegEx that will make sure that the first character after a space in a string is a `#`
RegEx that will make sure that the first character after a space in a string is a `#`

Time:10-07

Trying to build a regex to verify a string that would look something like this

#tag #tag2 #tag3 and not like this #tag1 tag2 tag3

I need a regex that will make sure that the first character after a space in the string is a #.

I have the check to make sure the first character is a # but that is as far as I have been able to get on this.

pattern: /^\#/

CodePudding user response:

You may use this regex:

^#[^#\s]*(?:\s#[^#\s]*)*$

RegEx Demo

RegEx Breakup:

  • ^: Start
  • #: Match a #
  • [^#\s]*: Match 0 or more of any characters that are not # and not a whitespace
  • (?:: Start non-capture group
    • \s: Match a whitespace
    • #: Match a #
    • [^#\s]*: Match 0 or more of any characters that are not # and not a whitespace
  • )*: End non-capture group
  • $: End
  • Related