Search code examples
regexphpstorm

Replace expression with subsection using regex?


My IDE PHPstorm allows you to do search and replace using regex, one of the things I find myself often doing is switching the order or action, aka, in function a I will set a value on items from list a using list b as the values.

but then in function b I want to invert it. so I want to set a value on items from list b using list a as the values.

A proper example is this:

var $clipDetailsGame        = $('#clipDetailsGame');
var $clipDetailsTitle       = $('#clipDetailsTitle');
var $clipDetailsByline      = $('#clipDetailsByline');
var $clipDetailsTeamOne     = $('#clipDetailsTeamOne');
var $clipDetailsTeamTwo     = $('#clipDetailsTeamTwo');
var $clipDetailsReferee     = $('#clipDetailsReferee');
var $clipDetailsDescription = $('#clipDetailsDescription');
var $clipDetailsCompetition = $('#clipDetailsCompetition');

function a(clip){
    clip.data('gameId'       , $clipDetailsGame.val());
    clip.data('title'        , $clipDetailsTitle.val());
    clip.data('byline'       , $clipDetailsByline.val());
    clip.data('team1'        , $clipDetailsTeamOne.val());
    clip.data('team2'        , $clipDetailsTeamTwo.val());
    clip.data('refereeId'    , $clipDetailsReferee.val());
    clip.data('description'  , $clipDetailsDescription.val());
    clip.data('competitionId', $clipDetailsCompetition.val()); 
}

function b (clip){
    $clipDetailsGame       .val(clip.data('gameId'));
    $clipDetailsTitle      .val(clip.data('title'));
    $clipDetailsByline     .val(clip.data('byline'));
    $clipDetailsTeamOne    .val(clip.data('team1'));
    $clipDetailsTeamTwo    .val(clip.data('team2'));
    $clipDetailsReferee    .val(clip.data('refereeId'));
    $clipDetailsDescription.val(clip.data('description'));
    $clipDetailsCompetition.val(clip.data('competitionId')); 
}

Excluding the formatting (It's just there to make my point clearer), what kind of regex could I use to do the replacement for me?


Solution

  • Basic regex -- nothing fancy or complex at all

    Search for: (clip\.data\('[a-zA-Z0-9]+')\s*, (\$[a-zA-Z0-9]+\.val\()(\)\);)
    Replace with: \$2\$1\$3

    The only PhpStorm-related thing here is replacement string format -- you have to "escape" $ to have it work (i.e. it has to be \$2 to use 2nd back-trace instead of just $2 or \2 (as used in other engines)).

    This will transform this:

    clip.data('gameId'       , $clipDetailsGame.val());
    clip.data('title'        , $clipDetailsTitle.val());
    clip.data('byline'       , $clipDetailsByline.val());
    clip.data('team1'        , $clipDetailsTeamOne.val());
    clip.data('team2'        , $clipDetailsTeamTwo.val());
    clip.data('refereeId'    , $clipDetailsReferee.val());
    clip.data('description'  , $clipDetailsDescription.val());
    clip.data('competitionId', $clipDetailsCompetition.val()); 
    

    into this:

    $clipDetailsGame.val(clip.data('gameId'));
    $clipDetailsTitle.val(clip.data('title'));
    $clipDetailsByline.val(clip.data('byline'));
    $clipDetailsTeamOne.val(clip.data('team1'));
    $clipDetailsTeamTwo.val(clip.data('team2'));
    $clipDetailsReferee.val(clip.data('refereeId'));
    $clipDetailsDescription.val(clip.data('description'));
    $clipDetailsCompetition.val(clip.data('competitionId')); 
    

    Useful link: http://www.jetbrains.com/phpstorm/webhelp/regular-expression-syntax-reference.html