-
-
Save illusionfield/da4ca68f222bfc82f735c46e46e1e589 to your computer and use it in GitHub Desktop.
js analog PHP str_replace()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.strReplace = function(find, replace) { | |
if(typeof find === 'string') | |
return this.split(find).join(replace); | |
var str = this; | |
for(var i in find) | |
str = str.split(find[i]).join(replace[i]); | |
return str; | |
} | |
// Test | |
var str = "<a href='http://$1/$2' title='$2'>$1</a>"; | |
console.info(str.strReplace("$1", "site.ru")); | |
// <a href='http://site.ru/$2' title='$2'>site.ru</a> | |
console.info(str.strReplace(["$1", "$2"], ["site.ru", "big-title"])); | |
// <a href='http://site.ru/big-title' title='big-title'>site.ru</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment