在 solidity 文件中查找结构

Find structure in solidity file

我在文本文件中有下一个结构: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;??

您需要将字符 class [()\w\s=>\d]+ 重复一次或多次以匹配左括号 ( 之后的 address => uint

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;,您应该在正则表达式后附加一个分号(您可以使 (public\s+) 成为非捕获组):

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