简体   繁体   English

Node.js中util.format()的补充功能

[英]Complementary function to util.format() in Node.js

I know how to format a string using %f, %d etc. using util.format(). 我知道如何使用util.format()使用%f,%d等格式化字符串。 Can someone tell me which is the complementary function that enables SCANNING from a string (not from console input). 有人可以告诉我,哪个是补充功能,可以从字符串(而不是从控制台输入)启用扫描。

For example: 例如:

Running... 正在运行...

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);

...generates... ... ...产生

The temperature at 5 o' clock was 23.9 deg. C and the humidity was 0.5.

I was expecting a util function which would work such that running the following code... 我期待一个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.`));

...generates... ... ...产生

0.5

Which is the util function that does this? 这是哪个util函数? I don't think "parseFloat" will work because it will extract 23.9. 我认为“ parseFloat”不起作用,因为它将提取23.9。

I'm new to JS and Node but I expected a "scan" function. 我是JS和Node的新手,但我希望使用“扫描”功能。 I know there is a scanf npm library but it seems to work with console input rather than existing strings. 我知道有一个scanf npm库,但它似乎可以在控制台输入而不是现有的字符串下工作。 I have been doing searches for "%f" among JS and Node functions and surprisingly util.format seems to be the only one with a mention of it. 我一直在JS和Node函数中搜索“%f”,令人惊讶的是util.format似乎是唯一提及它的人。

I don't know of any scan library like that, but you can use regular expressions. 我不知道像这样的任何扫描库,但是您可以使用正则表达式。 Here are some patterns you could use: 这是您可以使用的一些模式:

  • Integer: [+-]?\\d+ 整数: [+-]?\\d+
  • Decimal: [+-]?\\d+(?:\\.\\d+)? 十进制: [+-]?\\d+(?:\\.\\d+)?

If you put these in a capture group, you can access the corresponding matches from the array that String#match returns: 如果将它们放在捕获组中,则可以从String#match返回的数组中访问相应的匹配项:

 var weatherStr = "The temperature at 5 o'clock was 23.9 deg. C and the humidity was 0.5."; console.log(+weatherStr.match(/humidity was ([+-]?\\d+(?:\\.\\d+)?)./)[1]); 

You could create a utility function that can deal with %d and %f : 您可以创建一个可以处理%d%f的实用程序函数:

 function scanf(input, find) { var pattern = { "d": "(\\\\d+)", "f": "(\\\\d+(?:\\\\.\\\\d+)?)" }; find = find // Escape characters for use in RegExp constructor: .replace(/[-\\/\\\\^$*+?.()|[\\]{}]/g, '\\\\$&') // Replace %-patterns .replace(/((?:\\\\)*)%([az])/g, function (m, a, b) { return a.length % 4 == 0 && b in pattern ? a + pattern[b] : m; }); var match = input.match(new RegExp(find)); return match && match.slice(1).map(Number); } var weatherStr = "The temperature at 5 o'clock was 23.9 deg. C and the humidity was 0.5."; console.log(scanf(weatherStr, "humidity was %f")); console.log(scanf(weatherStr, "at %d o'clock was %f")); 

Thanks trincot! 谢谢小装饰品!

Actually it turns out the scanf npm library ( https://www.npmjs.com/package/scanf ) DOES solve my problem. 实际上,事实证明scanf npm库( https://www.npmjs.com/package/scanf )确实解决了我的问题。 I just hadn't read it all the way through. 我只是没有完全阅读它。 I had to install "sscanf" (note the double-s) as well. 我还必须安装“ sscanf”(注意double -s)。 The sscanf method (listed at the bottom of the package page) works just as I expected. sscanf方法(在软件包页面的底部列出)可以正常工作。

I'm surprised this package is not more popular, but it is what I need. 令我感到惊讶的是,这种包装没有受欢迎,但这正是我所需要的。 Thanks again! 再次感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM