html - Regex search & replace inline CSS style -


i need search & replace inside html tags, css inlined, in order avoid using style="" attribute inline.

i.e. replace looks this:

<table border="0" cellpadding="0" cellspacing="0" width="100%"  style="font-family: helvetica;line-height: 100%;margin-top: 20px; text-align: left;vertical-align: bottom;color: #202020"> 

into that:

<table border="0" cellpadding="0" cellspacing="0" width="100%" font-family="helvetica" line-height="100%" margin-top="20px" text-align="left" vertical-align="bottom" color="#202020"> 

does know regex search & replace have write in order that?

thanks.

use regex replacement:

(?:\g(?!^)|\bstyle=")([^:]*):\s*([^;]*)[;"](?=[^>]*>) 

replace (mind space @ end):

$1="$2"  

here demo

explanation

  • (?:\g(?!^)|\bstyle=") - boundary we'll start our matching. boundary end of previous match (\g(?!^)) or style=" (due \bstyle=").
  • ([^:]*) - 1st capturing group holds sequence of 0 or more characters other :
  • : - literal :
  • \s* - 0 or more whitespace
  • ([^;]*) - 2nd capturing group holds sequence of 0 or more characters other ;
  • [;"] - either ; or "
  • (?=[^>]*>) - check ending boundary make sure inside closing tag.

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -