Home > Enterprise >  find a string with a list of words, a char between them. and the final word can end with any string
find a string with a list of words, a char between them. and the final word can end with any string

Time:08-28

I have a list of words:

const words = ["apple", "orange", "tomato"]

const str = "apple.orange.tomato.$COULD_$_BE_ANY_STRING_HERE"

I am looking for the regex to validate this string structure.

  • the string cannot end with "."
  • the string must contain at least one "." char
  • the last string should support the "$" char

some patterns I've tried: ^((apple|orange|tomato)(\\.|$))*$ but it seems to allow the string to end with "." and to not contain a "."

CodePudding user response:

You can use this expression to start with one of the words at least one followed by dot ^((apple|orange|tomato)\.) ?, then optionally any letter or underscore \w*?, and not end with dot [^\.]$

^((apple|orange|tomato)\.) ?\w*?[^\.]$

TEST

  • Related