0

I have some text like

<<<<((((sports====1000))))>>>> 

or

<<<<((((sports====1000))))((((librarys====2000))))>>>> 

my problem is I have get the text from this like

text[0][0] = 'sports';
text[0][1] = '1000';
text[1][0] = 'library';
text[1][1] = '2000';

any help will be greatly appreciated. Thanks in advance.

3
  • Need some more clarity on what exactly do you want? Commented Nov 2, 2011 at 9:35
  • check out my answer. and tell me if its working ! Commented Nov 2, 2011 at 10:51
  • @HashimR I can't accept your ans because i have done not exactly but something like that. I want a more optimized solution. Felix Kling I have tried so far, is first i have the text within <<<<((((text))))>>>> than i have done some testing to check whether it is a single row or not by testing ))))(((( than i have splitted again by ==== than i got records. Sachin Shanbhag I like to grab the from strings like <<<<((((sp====1000))))>>>> one record or <<<<((((s====1000))))((((l====2000))))>>>> multiple record or <<<<((((libr====))))>>>> some entities can be empty any characters there can be. Commented Nov 2, 2011 at 11:22

3 Answers 3

2

You could try to split on everything but the content and then remove the empty elements in the array.

s="<<<<((((sports====1000))))((((librarys====2000))))>>>>";
s.split(/[<()>=]/).filter(function(ele){if (ele!="") return true});
=> ["sports", "1000", "librarys", "2000"]

This is kind of a hack and since I don't know the syntax of your text this might or might not work out for you.

Yet another hack:

s="<<<<((((sports====1000))))((((librarys====2000))))((((no_value====))))>>>>";
arr = s.replace("====)","====nil").
  split(/[<\(\)>=]/).
  filter(function(ele){if (ele!="") return true});
var res=[]; 
for(var i = 0; i < arr.length; i+=2) { res.push([arr[i],arr[i+1]]); }
res
=> [["sports", "1000"], ["librarys", "2000"], ["no_value", "nil"]]
Sign up to request clarification or add additional context in comments.

5 Comments

only works if the elements don't contain <,>,(,),= by it self. I guess the reason these separators are repeated 4 times could be to allow these characters (repeated less than 4 times) ;-) within the element data... beside this small objection I consider this a great solution.
It did work to some extend but when sorry that i didn't mention the text can be <<<<((((sports====))))((((librarys====2000))))>>>> means the amount can be null for that it didn't work and i did mention that i like to have it like a row record than it will be helpful to show result also can you explain the second line(s.split(/[<()>=]/).filter(function(ele){if (ele!="") return true});) of your code as I am a newbie
it can contain any element. But the number of element will not be the same. Like in '<<<<((((sports====1000))))((((librarys====2000))))>>>>' there not will be '<<<<((((sports========))))((((librarys====2000))))>>>>' or '<<<<((((sports========))))((((((((====2000))))>>>>'
I tried to explain the way it works in the answer but I can do another try .split(/[<()>=]/) splits the string to an array on every occurrence of <, (, ), and >. test="(1)2<3<(" would become ["", "1", "2", "3", "", ""]. Then it removes (filters) all elements in the array that are empty. That leaves the content. This method will not work if the data isn't always key=value. If there is a key but no value, it will break.
0

regex to get the elements could look like this:

\({4}(.+?)={4}(.+?)\){4}

see this example

2 Comments

did u try your solution ? I tried it before it will search greedily when I will apply for <<<<((((sports====1000))))((((librarys====2000))))>>>> it will return the whole ((((sports====1000))))((((librarys====2000))))
.+ is greedy but .+? as I noted is reluctant (non greedy). see the linked example...
0

Try this code.

var test  = "<<<<((((sports=====))))((((====2000))))((((hall====2000))))>>>>";
textAfterSplit1 = test.split(/[(<>)]/);

var myarray = [];

for (i=0; i <(textAfterSplit1.length); i++){
    textAfterSplit2 = textAfterSplit1[i].split(/[=]/);
    if (textAfterSplit2[0] == null){
        textAfterSplit2[0]= "";
    }
    if (textAfterSplit2[1] == null) {
        textAfterSplit2[1]= ""; 
    }
    myarray.push([textAfterSplit2[0],textAfterSplit2[1]]);
}

document.write(myarray[0][0]);
document.write(myarray[0][1]);
document.write(myarray[1][0]);
document.write(myarray[1][1]);

Intended output is:

sports2000

Its working in all scenarios. Hope this might help. Cheers!

Comments

Your Answer

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.