Skip to content Skip to sidebar Skip to footer

How To Replace Specific Parts In String With Javascript With Values From Object

I want to make custom replacer method for my HTML output. But I can't figure it out. I guess it should be done with String.match and replace somehow. I have some 'error codes' in m

Solution 1:

A simple regex should do the trick:

var content = content.replace(/\b_err_(.+?)\b/g, function(match, errorName) {
    returnLang[errorName] || match;
});

This assumes that you do not want strings like "blah_err_blah" to be replaced, and defaults to not replacing the text if the error cannot be found in your Lang object.

Solution 2:

var replace = function(str, object, regexp) { //if property not found string is not replacedreturnString(str).replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name) {
        return (object[name] != null) ? object[name] : match;
    });
}

This is a format function I've used in several projects thats quite efficient. Matches {prop} by default to {prop:'val'} but you can pass a regex for example maybe in your case /_err_+\S/g so it matches other tokens in your string.

So you can do:

var content ="Looks like you have {no_email} or {no_code} provided";
var Lang = {
    'no_email' : "No email",
    'no_code' : "No code"
}

var formatted = replace(content, lang);

Or for your original string stealing the other answers regex:

var formatted = replace(content, lang, /_err_([^\s]+)/g)

Solution 3:

You can use a callback function, that look if a key matching the error code exists in your Lang object, and if so returns the value of that (and otherwise just the key itself, thereby doing no replacement, like so:

content.replace(/_err_([a-z_]+)/gi, function(fullmatch, key) {
  return Lang[key] ? Lang[key] : fullmatch;
});

The first parameter passed to the function will be the full match, and the second parameter key will just be that part grouped by the bracktes.

And then, if Lang contains a (non-falsy) value for key, that’ll be returned, otherwise just the fullmatch value, so that that part of the string gets “replaced” with itself.

See it in action here: http://jsfiddle.net/VZVLt/1/

Solution 4:

One more variation with split

content = content
        .split('_err_')
        .map(function(str, index){
            if (index === 0) 
                returnstr;

            var whitespace = str.indexOf(' '),
                key = str.substring(0, whitespace)

            return Lang[key] + str.substring(whitespace);
        })
        .join('')
        ;

Post a Comment for "How To Replace Specific Parts In String With Javascript With Values From Object"