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 :

http://jsfiddle.net/s5y6msnb/1/


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -