1

Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?

This is my regexp

regexp:{
spamcheck    : /^[15]+$/
}

I need to do this

var spamcheck_sum = 15;

regexp:{
spamcheck    : /^[spamcheck_sum]+$/
}

and

Side note: I cant use my spamcheck_sum var outside the js reason is my js file loads first and my var for sum loads after , any way to globalize that var to be acceptable no matter if js file loads first?

working with cms here and js file placement is automatic if I use the cms js placements calls . Joomla

Community
  • 1
  • 1
Benn
  • 4,109
  • 7
  • 53
  • 99

1 Answers1

1
var spamcheck_sum = 15;

regexp:{
spamcheck    : new RegExp("^["+spamcheck_sum+"]+$")
}
Kamyar Nazeri
  • 23,046
  • 13
  • 46
  • 84
  • that is the one but just saw that I cant use it outside the file . reason is my js file loads first and my var for sum loads after , any way to globalize that var to be acceptable no matter if js file loads first? – Benn Feb 20 '12 at 19:41
  • 1
    How about a method that accepts check_sum as an input parameter and handles the rest? – Kamyar Nazeri Feb 20 '12 at 19:48
  • trust me I would not know how , this is a formcheck from mootools floor that I am modifying to put in Joomla module http://mootools.floor.ch/docs/formcheck/files/formcheck-js.html – Benn Feb 20 '12 at 19:53
  • but none the less I fund a way instead calling the js file with $document->addScript('') I used $document->addCustomTag('') this way I can place it anywhere , but it would be nice to know how to do this global var to be acceptable no matter of file position – Benn Feb 20 '12 at 19:54
  • @Benn: Not everything is possible. You can only use a value once it is set. – Felix Kling Feb 20 '12 at 19:59
  • 1
    I believe you have no control over page the loading! Normally scripts run as soon as they are loaded, using the defer attribute might help, it specifies that the script is executed when the page has finished parsing: – Kamyar Nazeri Feb 20 '12 at 20:02
  • good thinking! – Benn Feb 20 '12 at 20:07