You can not do it with regexes and probably you also do not want to write you own implementation of ecma-standard 262 (It is a total overkill).
As for me I dig google's V8 javascript engine, more precisely PyV8. I suggest you can use it.
If you had problems there is the code I used to install (pip installation had an error for my x64 system, so I used sources):
apt-get install subversion scons libboost-python-dev
svn checkout http://v8.googlecode.com/svn/trunk/ v8
svn checkout http://pyv8.googlecode.com/svn/trunk/ pyv8
cd v8
export PyV8=`pwd`
cd ../pyv8
sudo python setup.py build
sudo python setup.py install
As I remember these commands did not make errors for me. (I copypasted it but it worked)
Answer to the question itself:
More complex hello wolrd example, list some varibales of the global object:
import PyV8
class Global(PyV8.JSClass): # define a compatible javascript class
def hello(self): # define a method
print "Hello World"
def alert(self, message): # my own alert function
print type(message), ' ', message
@property
def GObject(self): return self
def __setattr__(self, key, value):
super(Global, self).__setattr__(key, value)
print key, '=', value
G = Global()
ctxt = PyV8.JSContext(G)
ctxt.enter()
ctxt.eval("var a=hello; GObject.b=1.0; a();")
list_all_cmd = '''for (myKey in GObject){
alert(GObject[myKey]);
}'''
ctxt.eval(list_all_cmd)
ctxt.leave()
(In browsers you should call you global object - Window)
This code will output:
b = 1
Hello World
<class '__main__.Global'> <__main__.Global object at 0x7f202c9159d0>
<class '_PyV8.JSFunction'> function () { [native code] }
<type 'int'> 1
<class '_PyV8.JSFunction'> function () { [native code] }
<class '_PyV8.JSFunction'> function () { [native code] }
<class '_PyV8.JSFunction'> function () { [native code] }
<class '_PyV8.JSFunction'> function () { [native code] }
<class '_PyV8.JSFunction'> function () { [native code] }
<class '_PyV8.JSFunction'> function () { [native code] }
<class '_PyV8.JSFunction'> function () { [native code] }
<class '_PyV8.JSFunction'> function () { [native code] }