Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RegEx that will make sure that the first character after a space in a string is a `#`

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 #.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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: /^\#/

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading