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 to match hashtag character with string format in javascript

I need a Regex to match a certain string format in javascript,

A hashtag symbol '#' has to be first and last character , There must be strings between these two characters,

If the string is more than one there must be two hashtag '##' symbol between each of them there is no limit to strings number.

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

Ex.

  1. #string1##string2##string3##…##string4#

  2. #string1#

  3. #string##string2#

I appreciate any help and assistance.

>Solution :

You can check the below regex with this pattern

#[^\#]+#: All characters (except #) between ##

(#[^\#]+#)+: At least a string matched the pattern

^ and $: Start and end of regex

const checkRegex = (value) => {
  const regex = /^(#[^\#]+#)+$/
  return regex.test(value)
}

console.log(checkRegex("#test")) //false
console.log(checkRegex("#test#")) //true
console.log(checkRegex("#test##")) //false
console.log(checkRegex("#test##test#")) //true
console.log(checkRegex("#test##test##")) //false
.as-console-wrapper { max-height: 100% !important; top: 0; }
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