javascript - Easiest way to replace one or another string with a value -
this question has answer here:
lets wanted change instances of united states or u.s. there more elegant way running several replaces:
function replaceus() { newstring = oldstring.replace("united states", "u.s."); newerstring = newstring.replace("us", "u.s."); } i know if needed check against huge list utilizing loop helpful, there more elegant solution if have few options check for. know following doesn't work, if did, trying describe. there analogous?
function replaceus() { newstring = oldstring.replace("united states" || "us", "u.s."); }
this version work multiples occurrences.
var x = 'this united states , us'; var r = x.replace(/(\bunited states\b|\bus\b)/g, "u.s."); console.log(r); you can see :
Comments
Post a Comment