470,841 Members | 1,159 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

regExp pattern

Hello

I am trying to read an aicc ini.file and storing each section and its
content into a new Object{}

[section]
prop1=value1
prop2=value2
prop3=value3
prop4=value4

[section2]
....
....

Would someone out there knows javaScript regExp and could give me one or two
hints on how to write a regex pattern?

Thanks a lot for your help

Roland
Oct 27 '05 #1
1 1125
JRS: In article <43**********@news.tiscalinet.ch>, dated Thu, 27 Oct
2005 21:39:48, seen in news:comp.lang.javascript, Roland <in**@gmm.ch>
posted :
Hello

I am trying to read an aicc ini.file and storing each section and its
content into a new Object{}

[section]
prop1=value1
prop2=value2
prop3=value3
prop4=value4

[section2]
...
...

Would someone out there knows javaScript regExp and could give me one or two
hints on how to write a regex pattern?


ISTM that a single RegExp, if possible, would be unduly complex.
<HEAD></HEAD><BODY><script>

S =
"[section0]\n" +
"prop1=value1\n" +
"prop2=value2\n" +
"\n" +
"[sectionA]\n" +
"propb=valueb\n" +
"propc=valuec\n"

S = S.replace(/\[(\w+)\]/g, " }, $1: {")
S = S.replace(/(\w+)=(\w+)/g, " $1:'$2',")
S = S.replace(/,\s+}, /g, "},\n\n ")
S = S.replace(/^\s+},/, "Obj = {")
S = S.replace(/,\s+$/, "} } ;\n")

document.write("<pre>",S) // which writes the following Object

Obj = { section0: { prop1:'value1', prop2:'value2'},
sectionA: { propb:'valueb', propc:'valuec'} } ;

</script></BODY></HTML>

To test, put that as a page and execute locally. To understand, comment
out all replace lines, and unveil in turn. Then adapt.

Not necessarily optimal.
*** DO NOT MULTIPOST ***

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 28 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bosconian | last post: by
1 post views Thread by python_charmer2000 | last post: by
5 posts views Thread by Ronald Fischer | last post: by
8 posts views Thread by Dmitry Korolyov | last post: by
6 posts views Thread by Edward | last post: by
6 posts views Thread by micklee74 | last post: by
11 posts views Thread by HopfZ | last post: by
2 posts views Thread by gypsy3001 | last post: by
2 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.