msqar
msqar

Reputation: 3020

How can I make this cases match with regex?

I need to match CSS as string.

  1. I don't want any # character (Id).
  2. Needs to match anything that includes as separate words "body", "li", "ol", "ul".
  3. Needs to detect the word alone "body", "li", "ol", "ul".
  4. Needs to detect the word among others i.e: "selector li".
  5. Needs to skip if a word contains "li, ol, ul" as part of it. For example "lololol" shouldn't match it.

I attempted doing it like this:

/^(\s)*|ol|ul|body|#|li|$/g

But is still not matching properly.

I'm using https://regex101.com/ to test it.

Upvotes: 0

Views: 28

Answers (1)

user4227915
user4227915

Reputation:

Just use \b to delimit the word boundary:

^(\s)*|\bol\b|ul|body|#|li|$(\s)*

Regex live here.

Upvotes: 1

Related Questions