Я работаю в квитанции.
У меня есть шаблон HTML как:
var Mytemplate= "Test Receipt
The receipt will be used tomorrow.
##start## A
B C
D
##end##
Here is more text"
Во время выполнения мне нужно заменить весь контент с «## start ##» до «## end ##», включая эти термины, на другую строку.
Я использую следующий код для извлечения текста:
String.prototype.extract = function(prefix, suffix) {
s = this;
var i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
}
else {
return '';
}
if (suffix) {
i = s.indexOf(suffix);
if (i >= 0) {
s = s.substring(0, i);
}
else {
return '';
}
}
return s;
};
var extracted_text=Mytemplate.extract("##start##","##end##");
var newContent=function(){
var newText=make_something_with(extracted_text)
return newText||"This is my new content"
}
Как я могу заменить содержимое с "## start ##" до "## end ##" на мой newContent?
Можно ли улучшить эту задачу с помощью Regex?