Search code examples
regex

Need a Regex pattern to return strings NOT containing a character


I need a regex pattern to return strings that contains 'bg-hh-' and not containing '/'.

Input:

.md\:hover\:bg-hh-graBla\/25:hover 
.md\:hover\:bg-hh-graBla\/30:hover 
.md\:hover\:bg-hh-graBla\/35:hover
.md\:hover\:bg-hh-graBla:hover

Output:

.md\:hover\:bg-hh-graBla:hover

Solution

  • You can use:

    ^[^\/\r\n]*bg-hh-[^\/\r\n]*$
    

    Note:

    You can add more restrictions, if necessary:

    • ^[^\/\r\n]*:bg-hh-[^\/\r\n]*$: You can add a :, before bg-hh-.
    • ^\.md\\:hover\\:[^\/\r\n]*bg-hh-[^\/\r\n]*$: This includes .md\:hover\: before bg-hh-.

    • [^\/\r\n] means all chars except those in the [].
    • * means zero or more times.