This should be easy for someone, I just can't seem to get the syntax correct. I have the following code, and I'm sure 70% of this can be represented by a loop: could someone enlighten me please?
function AddNewEmail(){
var jFilesContainer = $( "#emails" );
var jUploadTemplate = $( "#email-templates div.template" );
var jUpload = jUploadTemplate.clone();
var strNewHTML = jUpload.html();
var intNewFileCount = (jFilesContainer.find( "div.template" ).length + 1);
jUpload.attr( "id", ("emailedit[" + intNewFileCount + "]") );
strNewHTML = strNewHTML
.replace(
new RegExp( "::FIELD1::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD2::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD3::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD4::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD5::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD6::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD7::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD8::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD9::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD10::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD11::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD12::", "i" ),
intNewFileCount
)
;
jUpload.html( strNewHTML );
jFilesContainer.append( jUpload );
}
-
What doesn't work, what is the problem?Pekka– Pekka2011年05月09日 09:25:12 +00:00Commented May 9, 2011 at 9:25
3 Answers 3
If you're using regular expressions, use them:
strNewHTML = strNewHTML.replace(/::FIELD\d{1,2}::/gi, intNewFileCount);
answered May 9, 2011 at 9:27
Sign up to request clarification or add additional context in comments.
Comments
I would say
strNewHTML = strNewHTML.replace(/::FIELD\d+::/gi, intNewFileCount);
could be a replacement for your whole strNewHTML logic. Not a loop, but shorter anyway.
answered May 9, 2011 at 9:30
KooiInc
124k32 gold badges145 silver badges181 bronze badges
Comments
answered May 9, 2011 at 9:25
Atticus
6,75211 gold badges39 silver badges59 bronze badges
Comments
lang-js