Posted By

inamorix on 10/22/07


Tagged

js


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

gedittest
vali29
hans


String.prototype.matchIndexOf


 / Published in: JavaScript
 

  1. String.prototype.matchIndexOf = function (regexp) {
  2. var ptns = regexp.toString();
  3. var opt = ptns.match(/\/([a-z]*)$/)[1];
  4. ptns = ptns.replace(/^\/(.+)\/([a-z]*)$/, '$1');
  5. ptns = ptns.replace(/\\\|/ , '^^');
  6. ptns = ptns.split('|');
  7.  
  8. for (var i = 0, ix = ptns.length; i < ix; i++) {
  9. var ptn = new RegExp(ptns[i].replace(/\^\^/, '\\|'), opt);
  10. if (this.match(ptn)) {
  11. return i;
  12. }
  13. }
  14.  
  15. return -1;
  16. };

Report this snippet  

You need to login to post a comment.