简体   繁体   English

从JavaScript代码中提取字符串

[英]extracting strings from JavaScript code

Using embedded SpiderMonkey in my C++ application I would like to extract all the strings from JavaScript code. 在我的C ++应用程序中使用嵌入式SpiderMonkey,我想从JavaScript代码中提取所有字符串。 JavaScript code looks something like this: JavaScript代码如下所示:

var foo = "something";  
var space = " ";  
var bar = foo + space + "beautiful";  

C++ code looks like this: C ++代码如下所示:

char *script = "var foo = \"something\"; var space = \" \"; var bar = foo + space + \"beautiful\";";
ok = JS_EvaluateScript(cx, global, script, strlen(script), filename, lineno, &rval);

So my questions is, after SpiderMonkey executes JavaScript, how can I extract the string from variable bar (extracted value should be "something beautiful") and use it in my regular C++ code? 所以我的问题是,在SpiderMonkey执行JavaScript之后,如何从变量提取字符串(提取的值应该是“漂亮的东西”)并在我的常规C ++代码中使用它? I guess I have to evaluate the script first and then somehow extract the string from the JavaScript variable. 我想我必须先评估脚本,然后以某种方式从JavaScript变量中提取字符串。 I don't know how to extract the string using SpiderMonkey. 我不知道如何使用SpiderMonkey提取字符串。

My second question: 我的第二个问题:
http://siliconforks.com/doc/parsing-javascript-with-spidermonkey/ http://siliconforks.com/doc/parsing-javascript-with-spidermonkey/
This SpiderMonkey JavaScript parser is written for SpiderMonkey 1.6. 这个SpiderMonkey JavaScript解析器是为SpiderMonkey 1.6编写的。 How can this be done with latest SpiderMonkey, because APIs for parsing have changed? 由于用于解析的API已更改,如何使用最新的SpiderMonkey做到这一点?

Thnx in advance, 提前Thnx,
Goran 戈兰

Since bar is property of global object, after the JS_EvaluateScript() I can use JS_GetProperty() function, something like this 由于bar是全局对象的属性,因此在JS_EvaluateScript()之后,我可以使用JS_GetProperty()函数,如下所示

JS_GetProperty(cx, global, "bar", &rval);
JSString *str = JS_ValueToString(cx, rval);
printf("%s\n", JS_EncodeString(cx, str));

Check String operation functions like subString. 检查诸如subString之类的String操作函数。 It can be used to resolve your query. 它可以用来解决您的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM