Created
January 10, 2013 01:11
-
-
Save szmeku/4498549 to your computer and use it in GitHub Desktop.
Finds substring between
This file contains 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
RegExp.escape = function(s) { | |
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | |
}; | |
String.prototype.substrBetween = function(left, right, ignoreCase){ | |
left = RegExp.escape(left); | |
right = RegExp.escape(right); | |
ignoreCase = (ignoreCase == true) ? 'i' : ''; | |
var reg = new RegExp(left+"(.*?)"+right,'g' + ignoreCase); | |
return reg.exec(this); | |
} | |
// ========= Example of usage ============= | |
var s = "kopalniaPoszukiwanaUranu"; | |
console.log(s.substrBetween('kopalnia','uranu',true)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment