Created
September 26, 2011 14:19
-
-
Save DmitrySoshnikov/1242339 to your computer and use it in GitHub Desktop.
To explicit RegExp
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
/** | |
* toExplicitRe | |
* Converts implicit concatenations of RegExps into explicit ones | |
* by adding dot "." symbol as the concatenation symbol. | |
* | |
* The explicit format of RegExps is used by https://gist.github.com/1239804 | |
* | |
* Examples: | |
* | |
* - abc => a.b.c | |
* - a+bc => a+.b.c | |
* - a(bb)+c => a.(b.b)+.c | |
* | |
* by Dmitry Soshnikov <[email protected]> | |
* MIT Style License | |
* | |
*/ | |
function toExplicitRe(reStr, dontPrint) { | |
var explicitRe = reStr.replace(/([a-zA-Z0-9)*+])(?=[a-za-zA-Z0-9(])/g, "$1."); | |
!dontPrint && console.log(reStr, "=>", explicitRe); | |
return explicitRe; | |
} | |
// tests | |
toExplicitRe("abc"); // a.b.c | |
toExplicitRe("a+bc"); // a.b.c | |
toExplicitRe("a(bb)+c"); // a.b.c | |
toExplicitRe("a|b*c"); // a|b*.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment