Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js中util.format()的补充函数_Javascript_Node.js - Fatal编程技术网

Javascript Node.js中util.format()的补充函数

Javascript Node.js中util.format()的补充函数,javascript,node.js,Javascript,Node.js,我知道如何使用util.format()使用%f、%d等格式化字符串。有人能告诉我哪一个是支持从字符串扫描(而不是从控制台输入)的补充函数吗 例如: 运行 const util = require('util'); var weatherStr = util.format(`The temperature at %d o' clock was %f deg. C and the humidity was %f.`, 5, 23.9, 0.5); console.log(weatherStr);

我知道如何使用util.format()使用%f、%d等格式化字符串。有人能告诉我哪一个是支持从字符串扫描(而不是从控制台输入)的补充函数吗

例如:

运行

const util = require('util');
var weatherStr = util.format(`The temperature at %d o' clock was %f deg. C and the humidity was %f.`, 5, 23.9, 0.5);
console.log(weatherStr);
…产生

The temperature at 5 o' clock was 23.9 deg. C and the humidity was 0.5.
0.5
我希望有一个util函数,可以运行以下代码

const util = require('util');
var weatherStr = 'The temperature at 5 o' clock was 23.9 deg. C and the humidity was 0.5.';
console.log(util.????(tempStr, `humidity was %f.`));
…产生

The temperature at 5 o' clock was 23.9 deg. C and the humidity was 0.5.
0.5
哪个是执行此操作的util函数?我认为“parseFloat”不起作用,因为它将提取23.9


我不熟悉JS和Node,但我希望有一个“扫描”功能。我知道有一个scanfnpm库,但它似乎可以使用控制台输入,而不是现有的字符串。我一直在JS和节点函数中搜索“%f”,令人惊讶的是,似乎只有util.format提到过它。

我不知道有任何这样的扫描库,但你可以使用正则表达式。以下是您可以使用的一些模式:

  • 整数:
    [+-]?\d+
  • 十进制:
    [+-]?\d+(?:\。\d+)
如果将它们放在捕获组中,则可以从
String#match
返回的数组中访问相应的匹配项:

var weatherStr=“5点钟的温度为23.9摄氏度,湿度为0.5℃;
console.log(+weatherStr.match(/湿度为[+-]?\d+(?:\.\d+)。/)[1])谢谢trincot

事实上,scanfnpm库()确实解决了我的问题。我只是没有把它通读一遍。我还必须安装“sscanf”(注意双s)。sscanf方法(在包页面底部列出)的工作原理与我预期的一样

我很惊讶这个软件包不是很受欢迎,但它正是我所需要的。再次感谢