code_junkie

Javascript - MultiLine RegExp: lastIndex stuck on newlines?

Nov 13th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. >> rx = /^(.*)$/mg
  2.  
  3. >> tx = 'foonnbar'
  4.  
  5. >> rx.exec(tx)
  6. [foo,foo]
  7. >> rx.lastIndex
  8. 3
  9. >> rx.exec(tx)
  10. [,]
  11. >> rx.lastIndex
  12. 4
  13. >> rx.exec(tx)
  14. [,]
  15. >> rx.lastIndex
  16. 4
  17. >> rx.exec(tx)
  18. [,]
  19. >> rx.lastIndex
  20. 4
  21.  
  22. >> rx.lastIndex = 5
  23. 5
  24. >> rx.exec(tx)
  25. [bar,bar]
  26. >> rx.lastIndex
  27. 8
  28. >> rx.exec(tx)
  29. null
  30.  
  31. /^(.*)((rn|r|n)|$)/gm
  32.  
  33. /^(.*)[rn]*/gm
  34.  
  35. var rx = /^/gm,
  36. tx = 'AnBnC',
  37. m;
  38.  
  39. while(m = rx.exec(tx)){
  40. if(!m[0].length && rx.lastIndex > m.index){
  41. --rx.lastIndex;
  42. }
  43.  
  44. foo();
  45.  
  46. if(!m[0].length){
  47. ++rx.lastIndex;
  48. }
  49. }
  50.  
  51. ^(.*)$
  52.  
  53. ^(.+)$
  54.  
  55. ^(.*)n? // remove all r characters beforehand
  56.  
  57. ^(.*)(?:rn|nr|n|r)? // all possible CR/LF combinations, but *once* at most
Add Comment
Please, Sign In to add comment