-2

Here I want to get all classes as a string using regex.

  • What will be a good regex to match the below string.

Input String

   <div class="container mx-5">
      <button class="btn btn-primary mt-auto mb-8 mx-8 justify-center">Sign In</button>
      <button class="btn btn-secondary mt-auto mb-8 mx-8 justify-center">Sign Out</button>
    </div>

Expected Output from regex match

['class="container mx-5"', 'class="btn btn-primary mt-auto mb-8 mx-8 justify-center"', 'class="btn btn-secondary mt-auto mb-8 mx-8 justify-center"']
Meet Patel
  • 73
  • 1
  • 9
  • If you want to get the class attribute of every element with a class attribute, use `[...document.querySelectorAll('[class]')].map(n=>n.getAttribute('class'))`. This won't include the `class="..."` wrapper but you can re-add that if you really want to. – Niet the Dark Absol Jul 24 '21 at 10:30
  • "Expected Output from regex match" can you read? :) he want to – Pentiux Jul 24 '21 at 10:33
  • I can't do that, it's a runtime just in time compiler I am building for string find and replace functionality. – Meet Patel Jul 24 '21 at 10:33

1 Answers1

0

if you need only that what follows after class="

class=\"[^\>]*

if you need only in class=""

class=\"[^\"]*\"
Pentiux
  • 215
  • 3
  • 10