Skip to content

Converting HTML2React with Regex

Context

I was recently switching my HTML, CSS project to React JS. Down below are the some useful, but not extensible list of Regex I used. If you are considering doing the same, there is a better alternative to use JSX Converter like these.

Some Rules

FunctionSearch RegexReplace RegexComments
Change class to className and use module cssclass="([\w-]+)"className={galleryStyle.$1}galleryStyle is the name of the module css object
Adds extra backslash to img tag<img ([^>]+)><img $1 />
br tag<br><br/>
input tag<input ([^>]+)><input $1 />
Changing class names like ab-cd to abcdgalleryStyle[.]([\w]+)[-]([\w]+)galleryStyle.$1$2
Change naming semanticonclickonClick
Changing function calls in onClick to JSXonClick="([\w()'']+)"onClick={$1}
src="..([\w/.]+)"src="$1"
<!--[^(-->)]+-->
Convert inline CSS to normal ones (single property only)style="([\w]+)(?:[ ]*):(?:[ ]*)([\w#]+)(?:;)?"style={ {$1"$2"} }

Notes

Dealing with class="hi bro" | class="([\w-]+ )+[\w-]+" React has issues with normal DOM queries.