0

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.

asked Jun 25, 2015 at 5:48
2
  • 1
    And the question is? Commented Jun 25, 2015 at 5:59
  • You need to get the string you entered to return the two variables. Both those parts are explained. Commented Jun 30, 2015 at 1:12

1 Answer 1

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.

answered Jun 25, 2015 at 6:17
Sign up to request clarification or add additional context in comments.

7 Comments

Alright I'll give that a try tomorrow. Will that work if you paste the ENTIRE block of text in it though? Starting from the <!-- comment to the very end at _SectionZ"/>. It has to be able to parse that whole thing. Removing the comment tag and parsing out the rest.
Yes it works with the comment. Click "Run Code Snippet" for a demonstration. As you can see the from the code the comment is part of the string it is testing.
Alright so I gave it a go but am still having some issues. I made a Fiddle and shared a collaboration link and a share link to take a look at it. jsfiddle.net/ThePieMonster/xe1wactq/2/… jsfiddle.net/ThePieMonster/xe1wactq/2
You need to escape the brackets using &lt; and &gt; or the comment etc won't show up: jsfiddle.net/h4gx9x2x
Hello, again. Sorry for the delay, had a busy weekend. I updated a few things again and something is preventing the fiddle from running. jsfiddle.net/ThePieMonster/ju8zw8qq/3
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.