问题
我在正则表达式方面遇到了麻烦。
我有一组字符串。
示例字符串
@param* URL url './' an absolute URL giving the base location of the image
@param* Function callback callback(indexInArray, valueOfElement) The function that will be executed on every object.
@param Function callback Null The function that will be executed when the image is clicked
@param String name 'this is a test name' the name to use when displaying the image
@param String name "this is a test name" the name to use when displaying the image
我将它们按照换行符分割,以便得到单独的字符串。
var match = val.split("\n");
每个字符串都可以分解为五个值:
变量
例如:@param Function callback Null 当图像被点击时将执行的函数
@param = required;
Function = type;
callback = name;
Null = default;
The function that will be executed when the image is clicked = description.
我需要做的是将这个字符串分解成其部分,考虑到所有不同的字符串可能性,我很难创建一个可以在所有不同的默认值和描述可能性上拆分的正则表达式。
当defaultValue是双引号或单引号中的字符串或者函数符号时,我尤其难以取出它。
代码
这是我的当前工作代码;
$.each(match, function(index, value) {
var part = value.split(/\s/); //TODO need a regx to split this string correctly
var required = part[0];
var type = part[1];
var name = part[2];
var defaultValue = part[3];
var description = part[4];
});
总结
我需要一个单步或多步的正则表达式,可以将所有上面五个示例拆分成同样的五个变量。
编辑:我很抱歉没有尝试过正则表达式,但是我不确定从哪里开始。