Created
August 29, 2013 14:42
-
-
Save thisislawatts/6378995 to your computer and use it in GitHub Desktop.
Some funny little regexes I conjured up for an edge case project.
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.replaceAllButLast = function( search, replace ) { | |
var tmp = this.slice(0, this.lastIndexOf(search)), | |
end = this.slice(this.lastIndexOf(search)), | |
regex = new RegExp(search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), 'g'); // Escaping chars | |
return tmp.replace(regex,replace) + end; | |
} | |
String.prototype.replaceAllButFirst = function( search, replace ) { | |
var tmp = this.slice( this.indexOf(search)), | |
start = this.slice( 0, this.indexOf(search)), | |
regex = new RegExp(search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), 'g'); // Escaping chars | |
console.log(start); | |
return search + tmp.replace(regex,replace); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment