jeudi 19 septembre 2019

jquery replace part of a class name with pattern

I select all classes by class that begins with photo-. Replace part of class name by pattern. I need to replace photo-gallery-RID459852 with photo-gallery. Note: the part -RID[0-9] is replaced by ""

$("#Master [class*='photo-']").replace(function(index, css) {
  return (css.match(/(^|\s)-RID\S+/g) || []).join(' ');
}, "");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<main id="Master">
  <div class="photo-gallery-RID459852 other any some">
    Algo
  </div>
  <div class="photo-gallery-RID987410 other any some2 other2"></div>
  <div>
    <div>
      <div class="photo-gallery-369841 other any some"></div>
    </div>
  </div>
  <article>
    <div class="photo-gallery-RID36541 here now other any some"></div>
  </article>
</main>

My jsFiddle: https://jsfiddle.net/ngqku78p/

Aucun commentaire:

Enregistrer un commentaire