Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created May 22, 2009 05:50
Show Gist options
  • Select an option

  • Save subtleGradient/115953 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/115953 to your computer and use it in GitHub Desktop.
String.prototype.toRegExp = function(){
var match = this.match(/^\/(.*)\/([gimsx]{0,5})$/);
console.log(match)
return (match) ? new RegExp(match[1], match[2]) : new RegExp(this);
}
document.write("/flarm/gim" === /flarm/mig.toString());document.write('\n');
document.write("/flarm/gim".toRegExp().toString() === /flarm/mig.toString());document.write('\n');
document.write("/fl\
arm/gim".toRegExp().toString());document.write('\n');
document.write("/fl/////arm/gim".toRegExp().toString());document.write('\n');
document.write("/fl/////arm/gimsx".toRegExp().toString());document.write('\n');
document.write("/fl/////arm/gimsxxxxx".toRegExp().toString());document.write('\n');
document.write("/fl/////arm/".toRegExp().toString());document.write('\n');
document.write("/gim/gim/gim".toRegExp().toString());document.write('\n');
console.log(
"ab\
ab\
ab\
ab".match(/^(.*)$/gm)
)
console.log(
"ab\nab\nab\nab".match(/^(.)/gm)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment