Friday, August 24, 2012

Replace certain character JavaScript

To replace certain characters in javascript:

In this example, i'm replacing '&' with '&' in the entire string.


function formatTexts(linkText) {
    var patt1 = /&/g;
    var patt1replStr = "&";

    var matchPatt = linkText.match(patt1);

    var pattformat = /&/g;
    var matchformatted = "";
    if (matchPatt != null && matchPatt.length > 0) {
        while (matchformatted.length < matchPatt.length) {
            linkText = linkText.replace(linkText.match(patt1)[0], patt1replStr);
            matchformatted = linkText.match(pattformat);
        }
    }
    return linkText;
}

No comments:

Post a Comment