lundi 1 décembre 2014

Regex replace certain class names with JavaScript from a html document


I am trying to remove all the class names that end with an underscore. So:



<div class="foo bar_ baz boo_ fa"> </div>


would output:



<div class="foo baz fa"> </div>


I am trying to do this in JavaScript and so far I can do it if the input only contains the string without other stuff:



```Node.js
var input = "foo bar_ baz boo_ fa";
input = input.replace(/(\w*[^ ])(?=_)(_)/g, "");


outputs:



foo baz fa


I am guessing that I am not using look ahead correctly. I wish I could something like:


input = input.replace(/class\s*=\s*["'](\w*[^ ])(?=_)(_)/g, "");


Program's overall purpose:


The purpose of the script is to read the html document and remove the "bad" class names from the whole document...





Aucun commentaire:

Enregistrer un commentaire