So I have a website which has a text box that you can paste a block of text into. That string you entered is stored as a variable. I'm working on a javascript section that parses the string and leaves you with two variables. The part you past in looks something like the bit below.
<!-- Note: comment.
Comment/Comment, more comments "comment"-->
<xx:aaa xx:bbb="sss" xx:ccc="SectionA;SectionB;SectionC=SectionD;SectionE=SectionF.SectionG.SectionH;,SectionI=SectionJ;SectionK=SectionL;SectionM=SectionN;SectionO=SectionP.SectionQ.SectionR;SectionS=/;"/>
<vz:aaa xx:bbb="iii" xx:ccc="\SectionT\SectionU\_SectionV,\SectionW\SectionX,\SectionY\_SectionZ"/>
So enter the string and after the parse you will have the below variables:
varaible1: SectionA;SectionB;SectionC=SectionD;SectionE=SectionF.SectionG.SectionH;,SectionI=SectionJ;SectionK=SectionL;SectionM=SectionN;SectionO=SectionP.SectionQ.SectionR;SectionS=/;
variable2: \SectionT\SectionU\_SectionV,\SectionW\SectionX,\SectionY\_SectionZ
I have a basic parser set-up where it replaces \ with commas, but that isn't going to cut it obviously. There needs to be some serious parsing action going on here.
-
1And the question is?Dmitry Poroh– Dmitry Poroh2015年06月25日 05:59:20 +00:00Commented Jun 25, 2015 at 5:59
-
You need to get the string you entered to return the two variables. Both those parts are explained.Pie– Pie2015年06月30日 01:12:15 +00:00Commented Jun 30, 2015 at 1:12
1 Answer 1
You can parse the string using a regex:
var string = 'Note: comment. Comment/Comment, more comments "comment" \
<xx:aaa xx:bbb="sss" xx:ccc="SectionA;SectionB;SectionC=SectionD;SectionE=SectionF.SectionG.SectionH;,SectionI=SectionJ;SectionK=SectionL;SectionM=SectionN;SectionO=SectionP.SectionQ.SectionR;SectionS=/;"/> \
<vz:aaa xx:bbb="iii" xx:ccc="\SectionT\SectionU\_SectionV,\SectionW\SectionX,\SectionY\_SectionZ"/>';
var regex = /xx:ccc=\"([^\>]*)\"\/\>/g;
var variable1 = regex.exec(string)[1];
var variable2 = regex.exec(string)[1];
alert(variable1);
alert(variable2);
xx:ccc=\" match against the characters xx:ccc="
([^\>]*) capture multiple characters that can be anything except a close bracket
\"\/\> match against the characters "/>
regex.exec returns an array, the first element of which is the match and the second is the first capture group, so you can assign the contents of the capture group to your variables.