generated at
Replace Text UserScript


js
import '/api/code/progfay-pub/Replace_Text_UserScript/script.js'

文字列置換ができるUserScript
正規表現も使用できる
一度に複数個の置換を順次実行することも可能
書き方: (置換前の文字列/正規表現) => (置換後の文字列)
置換コマンドを並べた後には、空行を入れること

format
(before) => (after) (before) => (after) : (before) => (after) (text) (text) : (text)

sample1
/a|\s/g=>b ab a b a aba b a ba bb

sample2
/\s/g => fuga hoge hoge hoge

hr

script.js
const replaceText = text => { const splitPos = text.indexOf('\n\n') const commands = text.substr(0, splitPos).split('\n').map(command => { const [before = '', after = ''] = command.split(' => ', 2) return { before, after } }) let lines = text.substr(splitPos + 2).split('\n') for (const { before, after } of commands) { const match = before.match(new RegExp('^/(.*?)/([gimy]*)$')) const reg = match ? new RegExp(match[1], match[2]) : before lines = lines.map(line => line.replace(reg, after.replace(/\\n/g, '\n'))) } return lines.join('\n') } scrapbox.PopupMenu.addButton({ title: 'replace', onClick: replaceText })