gpt4 book ai didi

javascript - Node : RegExp returns nothing

转载 作者:行者123 更新时间:2023-11-30 06:17:02 26 4
gpt4 key购买 nike

我正在尝试从 Emotiv 输出中提取电极的名称及其值。然而,下面的代码不返回任何内容。

我是 nodejs 的新手。我试着用正则表达式在一行上做这件事,但它太复杂了,我做不好。我已经成功地捕获了整条线,但没有捕获值(value)。

这是我的代码:

var str = '"levels":{"F3":7094,"FC6":8209,"P7":12165,"T8":5380,"F7":1356,"F8":2043,"T7":11882,"P8":10117,"AF4":13257,"F4":6134,"AF3":13527,"O2":9686,"O1":871,"FC5":1808},"' ; 
const reg = new RegExp('.{2}\w\"\:\d{3,5}/g');
var test = str.match(reg) ;
if (test)
console.log(test[1]) ;

我希望 14 个电极的输出为 F3 : 8209 等等。

最佳答案

我知道这并不能完全回答 RegExp 问题,但这种重写应该会让你工作得更快。

const text = '"levels":{"F3":7094,"FC6":8209,"P7":12165,"T8":5380,"F7":1356,"F8":2043,"T7":11882,"P8":10117,"AF4":13257,"F4":6134,"AF3":13527,"O2":9686,"O1":871,"FC5":1808},"';

let json = "{" + text.substring(0, text.length - 2) + "}"
let obj = JSON.parse(json)

for (const [key, value] of Object.entries(obj.levels)) {
console.log(`${key} ${value}`);
}

我还提出了一个新的 RegExp,我认为它可以满足您的需求。

const text = '"levels":{"F3":7094,"FC6":8209,"P7":12165,"T8":5380,"F7":1356,"F8":2043,"T7":11882,"P8":10117,"AF4":13257,"F4":6134,"AF3":13527,"O2":9686,"O1":871,"FC5":1808},"';

let regexp = /\"([^\"]{2,3})\"\:(\d{3,5})/g
let r = null
do {
r = regexp.exec(text)
if (r) console.log(`${r[1]} ${r[2]}`);
} while (r)

关于javascript - Node : RegExp returns nothing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55525711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com