gpt4 book ai didi

Javascript 正则表达式 - 用空格、逗号和括号分隔单词的分割字符串

转载 作者:行者123 更新时间:2023-12-01 02:21:08 24 4
gpt4 key购买 nike

我正在尝试使用正则表达式拆分以下字符串,以便将每个单词、括号、逗号和空格分隔成字符串。我尝试了很多次,但找不到合适的模式。

var string = "WHEAT flour (49%) (with calcium, iron, niacin, thiamin), butter (MILK), pasteurised EGG, water, MILK, sugar, fresh yeast, salt, powdered pasteurised EGG"

var string = ["WHEAT"," ","flour"," ","(", "49%",")"," ","("," ",")" ......]

有人知道如何解决这个问题吗?

非常感谢您的帮助,尼科

最佳答案

根据您自己想要的输出,您真正需要的是:

([\s,()])

JS代码:

var string = "WHEAT flour (49%) (with calcium, iron, niacin, thiamin), butter (MILK), pasteurised EGG, water, MILK, sugar, fresh yeast, salt, powdered pasteurised EGG";
console.log(string.split(/([\s,()])/));

关于Javascript 正则表达式 - 用空格、逗号和括号分隔单词的分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49209278/

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