1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【サクラエディタ】スネーク⇔キャメル変換するマクロ

Posted at
snakeToCamell.js
function converter(match, offset, string){
  var inp = match;
  if(/^[A-Z0-9]+$/.test(inp)){
    return inp.toLowerCase();
  }
  //スネークケースかどうか?アンダースコアがあるか?
  if(/_/.test(inp)){
    //スネーク→キャメル
    //全て小文字に
    inp = inp.toLowerCase();
    //区切り文字の後を大文字に
    inp = inp.replace(/(_|^)(.)/g, snakeToCamelConv);
    //先頭を小文字に
    inp = inp.charAt(0).toLowerCase() + inp.slice(1);
  }else{
    //キャメル→スネーク
    //大文字の前に「アンダースコア」を追加
    inp = inp.replace(/([A-Z])/g, camelToSnakeConv);
    //全て大文字に
    inp = inp.toUpperCase();
  }
  return inp;
}

function snakeToCamelConv(match, p1, p2, offset, string){
  return p2.toUpperCase();
}

function camelToSnakeConv(match, p1, offset, string){
  return '_' + p1;
}

//(INPUT)選択範囲を取得
var str = Editor.GetSelectedString(0);
//(PROCESS)スネークケース⇔キャメルケース
str = str.replace(/(\w+)/g, converter);
//(OUT)編集したテキストを設定
Editor.InsText(str);

使用例

対象行を選択して、当マクロを実行する。

実行前

DATA1
SAMPLE_DATA2
SAMPLE_TEST_DATA3

実行後

data1
sampleData2
sampleTestData3

再実行後

DATA1
SAMPLE_DATA2
SAMPLE_TEST_DATA3
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?