0

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.

Ex.

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

  2. #string1#

  3. #string##string2#

4
  • What is your expectation on this? You want to print true/false values or all matched strings? Commented Apr 18, 2022 at 10:35
  • in your examples you havent provided the expected output..... Commented Apr 18, 2022 at 10:36
  • i need to print true if matches following format @NickVu Commented Apr 18, 2022 at 10:37
  • I need to print true if it matches with the formats in questiong @mrtechtroid Commented Apr 18, 2022 at 10:39

1 Answer 1

3

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; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.