此字符串
"START";var settings ={"dev":false,"production":"n","id":"bcdef","key":"1234xyz","secret":"22sdfas"}
; return { getSettings: function(setting) { if (settings[setting]) { return settings[setting]; } return null; } };"END"
如何用正则表达式替换它成为这个
"START";var __REPLACED = console.log('replaced');"END"
答案 0 :(得分:2)
我为你的目的写了这个函数:
/**
* This function assumes string as a argument and
* replaces everything between "START" and "END"
* with "START";var __REPLACED = console.log('replaced');"END"
* @param input string
* @author Georgi Naumov
* gonaumov@gmail.com for contacts and
* suggestions.
*/
function replaceString(input) {
var re = /^("START")(?:(?:(?!"END")[\s\S])+)("END")$/g;
return input.replace(re, function (match, p1, p2) {
return p1 + ";var __REPLACED = console.log('replaced');" + p2;
});
}
这里可以看到单元测试,它检查功能: