gpt4 book ai didi

javascript - 如何使用正则表达式或任何其他解决方案从复杂字符串中获取所有子字符串?

转载 作者:行者123 更新时间:2023-11-28 16:51:59 26 4
gpt4 key购买 nike

输入应该是这样的:

class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath

输出类似:

["class1{name='adam.smith'}", "class2{name='john'}", "subjectMath"]

有什么解决办法吗?

最佳答案

尝试使用split:

var input = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath";
var parts = input.split(/\.(?![^{]*’)/);
console.log(parts);

用于分割的正则表达式需要一些解释:

\.          match a literal dot
(?![^{]*’) but assert that we DON'T encounter a text curly quote
looking forward so long as we don't hit a { first

负向先行使 adam.smith 中的点失败,因为我们可以在不遇到 { 左括号的情况下找到大引号,这意味着该点不是连接点。

关于javascript - 如何使用正则表达式或任何其他解决方案从复杂字符串中获取所有子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59824888/

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