javascript: string.replace doesn't work

This question already has an answer here:

  • How to replace all occurrences of a string in JavaScript? 41 answers

  • Use regex.. JSFIDDLE

    By default replace will change only first occurrence of pattern. to replace all pattern in string you need to use regular expression with g modifier.

    contactPersons.append(contactPersonsTpl.replace(/%email%/g, contactPerson));
    

    variable contactPersonsTpl is string and not jquery object. thus you can not use .append() with it. You should use jquery object to use .append() . something like this:

    var contactPersonsTpl = $('#cm_contactPersons');
    contactPersonsTpl.append(contactPersonsTpl.html().replace('%email%', contactPerson));
    
    链接地址: http://www.djcxy.com/p/16842.html

    上一篇: Javascript字符全部替换

    下一篇: javascript:string.replace不起作用