Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Created September 26, 2011 14:19
Show Gist options
  • Save DmitrySoshnikov/1242339 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/1242339 to your computer and use it in GitHub Desktop.
To explicit RegExp
/**
* 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