Tag: string

在Javascript中使用随机数字和运算符生成一个随机math公式

我想创build一个程序,应该打印出最简单的mathexpressionforms,如(21 + 13)* 56使用随机号码。 1到100 ,程序必须采取一个级别参数, 级别决定生成方程的长度 ,例如: 游戏必须产生具有加法+乘法运算符的方程,如(21 + 13)* 56 (使用方括号) —-level 2 75 – 54 = 21 62 + 15 = 77 88 / 22 = 4 93 + 22 = 115 90 * 11 = 990 –level 3 ( 21 + 13 ) * 56 = 1904 82 – 19 + […]

string.replace不能在node.js express服务器上工作

我需要阅读一个文件,并用该dynamic内容replace该文件中的一些文本。当我尝试string.replace它不工作的数据,我从文件读取。但string它正在working.I使用节点。 js和快递。 fs.readFile('test.html', function read(err, data) { if (err) { console.log(err); } else { var msg = data.toString(); msg.replace("%name%", "myname"); msg.replace(/%email%/gi, 'example@gmail.com'); temp = "Hello %NAME%, would you like some %DRINK%?"; temp = temp.replace(/%NAME%/gi,"Myname"); temp = temp.replace("%DRINK%","tea"); console.log("temp: "+temp); console.log("msg: "+msg); } }); 输出: temp: Hello Myname, would you like some tea? msg: Hello %NAME%, […]

不能在node.js中使用模板string

根据MDN的说法, 模板string 应该在Chrome中运行,并且基于Node.js的扩展V8。 但是当我尝试以下我得到一个语法错误: var name = 'coffee'; console.log(`Hello, ${name}!`); 运行node file.js只会导致SyntaxError: Unexpected token ILLEGAL 有什么标志我需要启用使用此function,或者它只是没有实现节点?

Node.js:使用多个查询参数来表示app.get

我想查询yelp API,并有以下路线: app.get("/yelp/term/:term/location/:location", yelp.listPlaces) 当我向GET请求时 http://localhost:3000/yelp?term=food&location=austin , 我得到错误 Cannot GET /yelp?term=food&location=austin 我究竟做错了什么?

为什么JSON.stringify搞砸了我的date时间对象?

{ id: 1533, story_type_id: 1, content_id: 470, created_at: Sun, 05 Feb 2012 07:02:43 GMT, updated_at: Sun, 05 Feb 2012 07:02:43 GMT, type_name: 'post' } 我有一个像上面的“date时间”字段的JSON对象。 这是完美的。 但是,当我把它串起来(我想把它存储在caching中),我得到这种格式: "created_at":"2012-02-05T07:02:43.000Z" 这会导致问题,因为当我想JSON.parse这个,突然它不再是date时间格式,它与我的其他格式不兼容。 我能做些什么来解决这个问题? 我的应用程序中到处都是“created_at”。 我不想手动更改每一个。

Node.JS / Javascript – 将string转换为整数是当我不期望它返回NaN

这是一个更大的程序的背景下,所以我会尽量保持简单,只显示违规行。 我有一个值为数组的stringforms的数组拉,“84”,“32”等 然而,这条线 console.log(unsolved.length + " " + unsolved[0] + " " + parseInt(unsolved[0]) + " " + parseInt("84")); 打印: 4 "84" NaN 84 “84”是数组元素Im试图parseInt! 然而,除非我把它从数组的上下文中取出并明确地写出来,否则它将不起作用。 这是怎么回事?

将node.js中的字节数组发送到服务器

在连接到Debian上的服务器时,我无法想象发送字节数组。 有没有其他的发送string通过client.write() ? 我尝试client.write(new Buffer("something"))但这给Invalid data错误。 这是我的代码: var net = require('net'); function ModernBuffer(buffer) { // custom buffer class this.buffer = new ArrayBuffer(buffer.length); this.byteLength = this.buffer.byteLength; console.log('ModernBuffer.ByteLength: ' + this.byteLength); var Uint16View = new Uint16Array(this.buffer, 0, 1); for (var i=0; i<Uint16View.length; i++) { Uint16View[i] = buffer[i]; console.log("Entry " + i + ": " + Uint16View[i]); } […]

从Node.js中的数组中分组类似的string

比方说,我有一个数组中的不同的URL的集合: var source = ['www.xyz.com/Product/1', 'www.xyz.com/Product/3', 'www.xyz.com/Category/1', 'somestring'] 迭代数组并将类似的string分组到一个单独的数组中是什么方法? 上面例子中的期望输出是: var output = [ ['www.xyz.com/Product/1', 'www.xyz.com/Product/3'], ['www.xyz.com/Category/1'], ['somestring'] ]; 条件 source内的所有项目都可以是随机string 逻辑必须能够在有意义的时间比较和分组大约100000个项目 我find了string相似性库 ,它可以将一个string与一个string集合进行比较。 一种方法是迭代源代码,将每个项目与源集合进行比较,并应用规则对具有相似分数的项目进行分组。 不过我想这样做效率太差。 有人可以build议我一个有效的方法来完成我所需要的?

node.js与明确如何从url中删除查询string

我有一个button,执行到我的网页,并添加一个filter的查询string。 我的代码将filter应用于网格…但用户可以删除/编辑该filter。 由于他们可以看到在网格中应用了哪个filter,因此我想在显示页面时从查询string中删除?filter = blah。 (如果在页面上并且URL说明了,可能会引起混淆?filter = columnA最初是正确的,但是用户移除该filter并在columnB上应用新的filter….但是查询string仍然会显示?filter-columnA。网格可以处理改变filter,而不需要回发。)我该怎么做? 如果你不能删除/更新一个查询string,是否有可能parsing它,然后只是redirect到主页面没有查询string? 一旦我把filter保存到varfilter,我不再需要它在查询string。 这里是显示页面的代码 exports.show = function(req, res) { var filter = req.query.filter; if (filter === null || filter === "") { filter = "n/a"; } res.render("somepage.jade", { locals: { title: "somepage", filter: filter } }); };

Nodejs:将string转换为缓冲区

我试图写一个string到套接字(套接字称为“响应”)。 这里是我有sofar的代码(我试图实现一个字节caching代理…): var http = require('http'); var sys=require('sys'); var localHash={}; http.createServer(function(request, response) { var proxy = http.createClient(80, request.headers['host']) var proxy_request = proxy.request(request.method, request.url, request.headers); proxy_request.addListener('response', function (proxy_response) { proxy_response.addListener('data', function(x) { var responseData=x.toString(); var f=50; var toTransmit=""; var p=0; var N=responseData.length; if(N>f){ p=Math.floor(N/f); var hash=""; var chunk=""; for(var i=0;i<p;i++){ chunk=responseData.substr(f*i,f); hash=DJBHash(chunk); if(localHash[hash]==undefined){ localHash[hash]=chunk; toTransmit=toTransmit+chunk; }else{ […]