js 正则怼 let str = read_line(); str = '+' + str; // 给字符串前面加上个符号,方便用正则表达式提取 function strToInt(s) { // 自定义解析成整数的函数 let base = 10; if (s.charAt(1) === '0') { if (s.charAt(2) === 'x') { base = 16; } else { base = 8; } } return parseInt(s, base) } let result = 0; // 保存最终结果 const r = /[\+\-]\w*/g; //提取 + 号 或 - 号开头的包含 a-zA-Z0-9_ 这些的字符串 let m; while ((m = r.exec(str)) != null) { // 分组来,一个一个相加,即可输出结果 result += strToInt(m[0]); } printsth(result);
点赞 评论

相关推荐

烤点老白薯:可以 除了名字都偷了
点赞 评论 收藏
分享
牛客网
牛客企业服务