Coin163

首页 > 在 Node.js 中,如何向 MongoDB 数据库中储存一个 JavaScript 对象

在 Node.js 中,如何向 MongoDB 数据库中储存一个 JavaScript 对象

相关标签: node.js mongodb

up vote -5 down vote favorite

I have this string a,[b,c,d], e(f), g(h + i) = j, a + c = g(e)

How can I write a regex to parse it to array like below?

=> Array
     [1] => a
     [2] => [b,c,d]
     [3] => e(f)
     [4] => g(h + i) = j
     [5] => a + c = g(e)

Thanks in advance.

php regex share edited Jun 14 '14 at 5:36 asked Jun 12 '14 at 9:25 PhatHV 4,702 4 21 37

closed as unclear what you're asking by OGHaza, Robin, 웃웃웃웃웃, hjpotter92, Sergey Telshevsky Jun 13 '14 at 9:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

7   Given you have almost 3k rep, you should know better than to post a "gimme teh codez" question... –  OGHaza Jun 12 '14 at 9:27 1   This seems like the wrong approach, why can't you change your data? all you need is a space after that first comma. –  fin Jun 12 '14 at 9:31      The space in my string is not important. So, it can be every where not only after the first comma. –  PhatHV Jun 12 '14 at 9:37 1   Are nested strucures like a,[b,[c,d],e] a possibility? –  Taemyr Jun 12 '14 at 10:05 1   How about empty entries like a,,b,c,d? Or other edge cases? - Notwithstanding the current answers, for forming suitable expressions one needs to know what assumptions can be made about the input. –  Taemyr Jun 12 '14 at 10:19  |  show 4 more comments

2 Answers 2

active oldest votes up vote 2 down vote accepted
/(\[[^]]+\]|[^,]+)/g

http://regex101.com/r/xV2uA6

use preg_match_all to get the matches

http://www.php.net/preg_match_all

share edited Jun 12 '14 at 10:14 answered Jun 12 '14 at 9:56 user887675 517 2 6 1   You should use [^]]+ instead of .+ or you'll match too much in [a,b],c,[d]. Though we know nothing of the possible input... –  Robin Jun 12 '14 at 10:01      Good point, edited the answer. –  user887675 Jun 12 '14 at 10:15      I trimmed all space and used below RE for expanded cases like (a,b,c),g(a,b,c),g(a,b)=f(c,d),g(a,b)=f(c,d)+h(e,f),g(a,b)=f‌​(c,d)+h(e,f),g(a,b)-‌​f(c,d)=h(e,f),g(a,b)‌​*f(c,d)+h(e,f)=f(t,y‌​). the RE is /(([|()[^])]+(]|))|[^,]?\w*([^)]+()=‌​(?R)|)-(?R)|)‌​;+(?R)|)*(?R)|&#‌​41;\/(?R)|)|)|[^‌​,]+)/g –  PhatHV Jun 13 '14 at 7:29      (([|()[^])]+(]|))|([^,]?\w*(|[^,]?[\w.]*‌​=[\w.]*()[^)]+(&‌​#41;=(?R)|)-(?R)‌​|)+(?R)|)*(?‌​R)|)\/(?R)|)‌​|)|[^,]+) –  PhatHV Jun 13 '14 at 8:04 add a comment  |  up vote 1 down vote

The following regexp returns a match for each of the entries in your array.

/(?<=^|,)(?:([^\[\],]*)|(\[([^\[\]]*)\]))(?=$|,)/g
share edited Jun 12 '14 at 10:03 answered Jun 12 '14 at 9:58 Taemyr 1,805 6 15      Your answer also solved my problems,too. –  PhatHV Jun 12 '14 at 10:22 add a comment  | 

Not the answer you're looking for? Browse other questions tagged php regex or ask your own question.

原文

我希望在 MongoDB 中储存一个 Mongo 查询,比如可能类似下面这样:{ $set: { account_id: ObjectID('525284cc2cebb6d0008b4567') }}但是我并不能直接储存在 MongoDB 中,

------分隔线----------------------------
相关推荐