Search code examples
regexphpstorm

Regex find CamelCase strings and dashes, replace with lowercase_underscored


In PHPStorm, I need to find/replace some mixed case strings which are used for CSS class names and for the DOM id's. I can't change attributes like onClick and image names need to remain. Here is what I have:

<div class="ThumbContainer" id="Source-Data4-Thumb">
<div class="ThumbTitleArea">
    <div class="DataTitleDiv"> GYR Performance <img src="images/someImage.png" onClick="someFunc()" /></div>
</div>
    <div class="dataDetailArea">
    <div class="DataThumbArea"> Data Source:Client<br>
                    Last refreshed:12/05/2013 <br>
                    Records:206<br>
        <br>
                        Used for the following reports<br>
                -    GYR Performance<br>
        </div>
</div>
</div>

Here is what I need:

<div class="thumb_container" id="source_data4_thumb">
<div class="thumb_title_area">
    <div class="data_title_div"> GYR Performance <img src="images/someImage.png" onClick="someFunc()" /></div>
</div>
    <div class="data_detail_area">
    <div class="data_thumb_area"> Data Source:Client<br>
                      Last refreshed:12/05/2013 <br>
                      Records:206<br>
        <br>
                          Used for the following reports<br>
                 -    GYR Performance<br>
        </div>
</div>
</div>

Notice the dataDetailArea starts with a lowercase.. bleh. This will be a one-time find/replace so it doesn't need to be in PHPStorm. It can be in any online tool even, like http://gskinner.com/RegExr/

The actual backbone template I need to find/replace on is about 3100 lines of code, otherwise I'd provide it all here for you.

Here's what I have so far. It seems to not match match the Camel-Case3-Foo:

(class|id|data-[?!=])="\b([A-Za-z][a-z-]*){2,}\b"

Solution

  • This regex should find the locations where underscores should be placed:

    ((?<=\w)(?=[A-Z])|-)
    

    It would seem to make sense to do a replacement with this to insert the underscores, then convert the string to lower case.