gpt4 book ai didi

javascript - JS : Extracting specific text from a string

转载 作者:行者123 更新时间:2023-11-29 18:52:04 27 4
gpt4 key购买 nike

我有一个这样的字符串......

var str = "6 validation errors detected: Value '' at 'confirmationCode' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+; Value '' at 'confirmationCode' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'password' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+; Value at 'password' failed to satisfy constraint: Member must have length greater than or equal to 6; Value at 'username' failed to satisfy constraint: Member must satisfy regular expression pattern: [\p{L}\p{M}\p{S}\p{N}\p{P}]+; Value at 'username' failed to satisfy constraint: Member must have length greater than or equal to 1";

我想提取所有以单词...“值”...开头的“唯一”行

所以预期的输出是...

  • 'confirmationCode' 的值 '' 未能满足约束条件
  • 'password' 的值不满足约束条件
  • “用户名”的值未能满足约束

这是我到目前为止尝试过的...

var x = str.split("\;|\:")  // This is NOT working
console.log(x);

var z = y.filter(word => word.indexOf("Value") > -1) // Also this needs to be tweaked to filter unique values
console.log(z);

性能是个问题,所以我更喜欢最优化的解决方案。

最佳答案

您可以使用单个正则表达式,不需要splitfilter 或循环或其他需要的测试:

var str = "6 validation errors detected: Value '' at 'confirmationCode' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+; Value '' at 'confirmationCode' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'password' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+; Value at 'password' failed to satisfy constraint: Member must have length greater than or equal to 6; Value at 'username' failed to satisfy constraint: Member must satisfy regular expression pattern: [\p{L}\p{M}\p{S}\p{N}\p{P}]+; Value at 'username' failed to satisfy constraint: Member must have length greater than or equal to 1";

console.log(
str.match(/((^|Value)[^:]+)(?!.*\1)/g)
);

关于javascript - JS : Extracting specific text from a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50904353/

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