用“函数"解析字符串的JavaScript [英] Javascript to parse string with 'function'
问题描述
我有以下字符串,需要使用JS进行解析.
I have following string which needs to be parsed using JS.
function () {\n MyClass.prototype.doSomethingFunction();\n 5-6 lines of coding\n }
我尝试解析它,修剪空格和换行符,但对我没有任何帮助.请让我知道如何从上述字符串中获取函数名称(doSomethingFunction).
I tried parsing it, trimming whitespaces and newlines but nothing worked for me. Please let me know how can I get function name (doSomethingFunction) from the above string.
我如何获取此字符串:
我有一个存储函数的队列.稍后,我的代码从此队列中选择一个函数(此处为一些逻辑)并执行它们.对我来说一切正常.但是我只想从中打印出函数的名称!就像'(classOBJ.myFunctionsQueue [n])()'用于执行存储在myFunctionsQueue数组中第n个位置的函数一样.有道理还是我在这里做错了什么?
I have a queue where my functions are stored. Later in time my code picks a function from this queue (some logic here) and execute them. It works perfectly ok for me. But I just want to print the name of the function out of it! It's like '(classOBJ.myFunctionsQueue[n])()' is used to execute a function which is stored at nth location in myFunctionsQueue array. Make sense or I'm doing something wrong in here?
谢谢 曼
推荐答案
尝试匹配这样的reg表达式
Try a reg expression matching like this
var str = "function() {\n MyClass.prototype.doSomethingFunction();\n 5 - 6 lines of coding\n}";
var matches = str.match(/prototype\.(.+?)\(\)/);
if(matches){
alert(matches[1]);
}
这篇关于用“函数"解析字符串的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!