在solidity文件中查找结构

时间:2018-04-06 14:11:05

标签: javascript regex nsregularexpression solidity

我在文本文件中有下一个结构:mapping(address => uint) public lockedAddresses;我希望找到它。我尝试使用下一个正则表达式:mapping\s*\([\w\s=>\d]\)\s+(public\s+)?lockedAddresses。我不明白我怎么能找到它。

var string = 'event Release();\n address public holder;\n	mapping(address => uint) public lockedAddresses;\n 	modifier isReleased () {\n	require(released || msg.sender == holder || msg.sender == owner);\n		require(lockedAddresses[msg.sender] <= now);\n		_;\n	}';

var position = string.search(/mapping\s*\([()\w\s=>\d]\)\s+(public\s+)?lockedAddresses/);

console.log(position);

如何在文本中找到结构:mapping(address => uint) public lockedAddresses; ??

1 个答案:

答案 0 :(得分:2)

您需要重复一次或多次字符类[()\w\s=>\d]+,以便在左括号address => uint后匹配(

mapping\s*\([()\w\s=>\d]+\)\s+(public\s+)?lockedAddresses
                        ^

&#13;
&#13;
var string = 'event Release();\n address public holder;\n	mapping(address => uint) public lockedAddresses;\n 	modifier isReleased () {\n	require(released || msg.sender == holder || msg.sender == owner);\n		require(lockedAddresses[msg.sender] <= now);\n		_;\n	}';
var position = string.search(/mapping\s*\([()\w\s=>\d]+\)\s+(public\s+)?lockedAddresses/);
console.log(position);
&#13;
&#13;
&#13;

要完全匹配mapping(address => uint) public lockedAddresses;,您应该在正则表达式后添加分号(并且可以使(public\s+)成为非捕获组):

mapping\s*\([()\w\s=>\d]+\)\s+(?:public\s+)?lockedAddresses;