0
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
plus1.js
//☆使い方☆
//①エクセルの1行目に置換前、2行目に置換後を記入。
//②対象データ範囲をコピーし、クリップボードに登録。
//③処理対象文字列を選択し、マクロ実行。
var listConv = function (inStr, inClip){
  var convList = inClip.split('\r\n');
  for(var i in convList){
    var convRecord = convList[i].split('\t');
    var oldStr = convRecord[0];
    var newStr = convRecord[1];
    if(typeof oldStr === 'string' 
      && typeof newStr === 'string'){
      inStr = inStr.replace(new RegExp(oldStr,'g'), newStr);
    }
  }
  return inStr;
}
//(INPUT)選択範囲を取得
var str = Editor.GetSelectedString(0);
//(INPUT)クリップボードを取得
var clip = Editor.GetClipboard(0);
//(PROCESS)クリップボードの文字列を利用して、順次置換
str = listConv(str, clip);
//(OUT)編集したテキストを設定
Editor.InsText(str);

使用例

置換データをエクセルからコピーし、クリップボードに登録。
置換データ.PNG

サクラエディタの処理対象行(2~5行)を選択して、当マクロを実行する。

実行前

TEST1
TEST1TEST2TEST3
TEST44TEST55TEST66
TEST777TEST888TEST999
TEST10
TEST1

実行後

TEST1
TEST①TEST②TEST③
TEST④④TEST⑤⑤TEST⑥⑥
TEST⑦⑦⑦TEST⑧⑧⑧TEST⑨⑨⑨
TEST⑩
TEST1
0
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
0
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?