1

I've been struggling with this for the last few days. I apologize if this is a duplicate, I wasn't able to find what I needed when searching for this particular question.

I have class names like the following:

class="block underline primary"

className="text-center block primary-dark"

class="grey bg-black inline-block block"

I'd like to search an entire codebase using Atom's regex search feature and replace every instance with a new class name. I would need the following rules:

  1. Make sure the string is contained in class="" or className=""
  2. Make sure it's only matching the exact word, so in the above it would only match block and not inline-block if that's what I was searching for.

I currently have this which almost does what I need, but isn't accounting for className or class and will return paragraphs or things not contained within a class which I don't want:

(\s(block)\s)|(="(block)\s)|(\s(block)")

Is there any way to do a regex find and replace in one fell swoop? I understand I might not get everything because classes can be programmatically added, but I'd like to get as much as possible with find and replace and not screw other things up. Any help or direction is greatly appreciated.

edit

I also need to account for class names like the following:

class="block block-title blockDisabled"

So in the end I only want to target block and nothing else.

souporserious
  • 2,079
  • 2
  • 27
  • 47

1 Answers1

1

You could use the following expression:

(className|class)="(?:block|block\s([^"]*)|([^"]*)\sblock|([^"]*)\sblock(?=\s)([^"]*))"

Live Example Here

From there, if you want to remove the block class, you would use $1="$2$3$4$5" for the replacement.

However, if you want to replace the block class, as your title implies, then you would use $1="$2$3$4$5 replacement-class" for the replacement (where the string "replacement-class" is the class you're replacing the block class with).

Explanation:

  • (className|class) - Capture the attribute name
  • =" - Match the opening of the attribute value
    • (?: - Start of a non-capturing group
      • block - Match the string block
      • | - or...
      • block\s([^"]*) - Capture the classes after the string block
      • | - or...
      • ([^"]*)\sblock - Capture the classes before the string block
      • | - or...
      • ([^"]*)\sblock(?=\s)([^"]*) - Capture the classes around the string block
    • ) - End of the non-capturing group
  • " - Match the closing of the attribute value
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
  • Thank you for the quick reply! Sorry, I should have mentioned that class names like `block-title` or `blockOverlay` can be included that I wouldn't want to target, see here: https://regex101.com/r/xICKkv/1 I would like to just replace instances of "block" with nothing surrounding it. Is that possible? – souporserious Feb 12 '17 at 19:39
  • @ftntravis - I think I got it... I added an explanation; here is the [updated example](https://regex101.com/r/fFBTRU/1)... – Josh Crozier Feb 12 '17 at 20:25
  • 1
    Thank you so much!!! This seems to be working perfect. I really appreciate the help. This will save so much time! :D – souporserious Feb 12 '17 at 20:26