Last active
August 29, 2015 14:15
-
-
Save nikolaifedorov/51735c7a807b0a635755 to your computer and use it in GitHub Desktop.
solution for Ex. 1 https://github.com/ivanStraltsou/code-wars/tree/master/katas/regexp
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.clone = function (regExp) { | |
var pattern = regExp.source, | |
isGlobal = regExp.global, | |
isIgnoreCase = regExp.ignoreCase, | |
isMultiline = regExp.multiline; | |
flags = ''; | |
flags += isGlobal ? 'g' : ''; | |
flags += isIgnoreCase ? 'i' : ''; | |
flags += isMultiline ? 'm' : ''; | |
return new RegExp(pattern, flags); | |
}; | |
RegExp.prototype.clone = function () { | |
return RegExp.clone(this); | |
}; | |
// Example: | |
/* | |
* var regex = /^[abc]/gim, | |
* cloneRegex1 = regex.clone(), | |
* cloneRegex2 = RegExp.clone(regex); | |
* | |
* | |
* regex.exec("ZZ\nZZCZ\nCZZ\na"); // => ["C"] | |
* cloneRegex1.exec("ZZ\nZZCZ\nCZZ\na"); // => ["C"] | |
* cloneRegex2.exec("ZZ\nZZCZ\nCZZ\na"); // => ["C"] | |
* | |
* regex.exec("ZZ\nZZCZ\nCZZ\na"); // => ["a"] | |
* cloneRegex1.exec("ZZ\nZZCZ\nCZZ\na"); // => ["a"] | |
* cloneRegex2.exec("ZZ\nZZCZ\nCZZ\na"); // => ["a"] | |
* | |
* regex === regex // => true | |
* regex === cloneRegex1 // => false | |
* regex === cloneRegex2 // => false | |
* cloneRegex1 === cloneRegex2 // => false | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Если нужно сохранить и позицию, то