小编Chu*_* Fu的帖子

如何在浏览器中使用webtorrent?

我在https://github.com/feross/webtorrent#usage中显示的示例有一些问题 我正在尝试在浏览器中使用该代码.所以我首先创建一个名为app.js的文件

app.js

var WebTorrent = require('webtorrent')
var concat = require('concat-stream')

var client = new WebTorrent()
console.log('Hi there');
client.download('magnet:?xt=urn:btih:XXXXXXXX', function (torrent) {
  // Got torrent metadata!
  console.log('Torrent info hash:', torrent.infoHash)

  torrent.files.forEach(function (file) {
    // Get the file data as a Buffer (Uint8Array typed array)
    file.createReadStream().pipe(concat(function (buf) {

      // Append a link to download the file
      var a = document.createElement('a')
      a.download = file.name
      a.href = URL.createObjectURL(new Blob([ buf ]))
      a.textContent = 'download ' + file.name
      document.body.appendChild(a)
    }))
  })
}) …
Run Code Online (Sandbox Code Playgroud)

html javascript browserify webtorrent

6
推荐指数
1
解决办法
6833
查看次数

如何将字符串化的正则表达式文字转换回正则表达式?

例如,我在客户端从服务器获取了一个字符串:

"/hello\s{0,1}[-_.]{0,1}world|ls\b/gim"
Run Code Online (Sandbox Code Playgroud)

在客户端,我想将此字符串转换为正则表达式对象。我试过

new RegExp("/hello\s{0,1}[-_.]{0,1}world|ls\b/gim")
Run Code Online (Sandbox Code Playgroud)

但这行不通,返回的对象是

/\/hellos{0,1}[-_.]{0,1}world|ls\/gim/
Run Code Online (Sandbox Code Playgroud)

总结一下:我想要的是:

/hello\s{0,1}[-_.]{0,1}world|ls\b/gim.test('hello world') //true (correct behavior)
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用:

new RegExp("/hello\s{0,1}[-_.]{0,1}world|ls\b/gim").test('hello world') //false
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?

javascript regex literals regex-group

3
推荐指数
1
解决办法
3819
查看次数