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.