[英]Javascript split string using regex
I'm working in javascript and I have the following string I'd like to split using the split() function:我在 javascript 中工作,我想使用 split() 函数拆分以下字符串:
'{'hello', 'hi'},{'hi', 'hello'}'
Into: Array: [{'hello', 'hi'}, {'hi', 'hello'}]
成: Array: [{'hello', 'hi'}, {'hi', 'hello'}]
My problem in doing this split is that there are other commas, and I want to split it on the particular comma between the two },{
.我在进行此拆分时遇到的问题是还有其他逗号,我想将它拆分为两个},{
之间的特定逗号。 The regular expression I tried to use is /(?<=}).*(?={)/g
, but it's giving me errors when I try to run it.我尝试使用的正则表达式是/(?<=}).*(?={)/g
,但是当我尝试运行它时它给了我错误。 Let me know which expression to use.让我知道该使用哪种表达方式。 Any help is greatly appreciated!任何帮助是极大的赞赏!
The regex you're looking for is /{(.*?)}/g
and if you use it in a string.match()
method, you'll get the array of {} you're looking for.您正在寻找的正则表达式是/{(.*?)}/g
并且如果您在string.match()
方法中使用它,您将获得您正在寻找的 {} 数组。
let str = `{'hello', 'hi'}, {'hi', 'hello'}` let arr = str.match(/{(.*?)}/g) console.log(arr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.