0

I have a JS var that holds string whose content is like this Automated - UI & Functional Regression on iPhone 6 running iOS 9. Similarly I have other string values as:


Automated - Under the Hood: iPhone 6 running iOS 9  

Automated - AU/NZ:  iPhone 6 running iOS 9

Automated - DRM: iPhone 6 running iOS 9 

Automated - Ads regression: iPhone6 running iOS 9

Automated - Ads regression: iPhone 5 running iOS 8 

Automated - UI & Functional Regression on iPad 2 running iOS 8

Automated - Under the Hood: iPad Air on iOS 8

Automated - AU/NZ: iPad Air running iOS 8

Automated - DRM iPadAir running iOS 8



Here is JS var:

var dump = test.name //test.name = "Automated - UI & Functional........"

How do I fetch the specific content in this dump var such as UI & Functional, iPhone 6, iOS, 9 and assign each of them to different var further and finally print? Here 9 is the OS version.

Idea is to print eventually in JS as:

Type: UI & Functional regression
Phone: iPhone 6
OS: iOS 
OS version: 9

Similarly for other above mentioned strings.

What I tried or can think of:

1. Use the JS split() function

2. Is it possible to put the var string content in an array and then split()?

4 Answers 4

1

You could do this:

    var arystrFields = test.split("-")
       ,strAfterAutomated = arystrFields[1].trim();

You need a more rigid format for the string to extract things further, use a delimiter such as : to split the other fields, then you could do this to split the remaining fields:

    var arystrRemaining = arystrFields[1].split(":");
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure if I get that: Let's say, I have test var as var test = "Automated - Under the Hood: iPhone 6 running iOS 9" only. For this example, I want to have output as Type: Under the Hood then on next line Phone: iPhone 6 then on next line OS type: iOS then on next line OS version: 9.
Nvm, got this - fixed.
1

You can achieve it to use regex. Look this plunker : https://plnkr.co/edit/Thk5j4ZqxSklTKTubQ40?p=preview

There is two simple regex :

var test = 'Automated - Under the Hood: iPhone 6 running iOS 9 \n\r' +
           'Automated - AU/NZ:  iPhone 6 running iOS 9\n\r' +
           'Automated - DRM: iPhone 6 running iOS 9\n\r' +
           'Automated - Ads regression: iPhone6 running iOS 9\n\r' +
           'Automated - Ads regression: iPhone 5 running iOS 8\n\r' +
           'Automated - UI & Functional Regression on iPad 2 running iOS 8\n\r' +
           'Automated - Under the Hood: iPad Air on iOS 8\n\r' +
           'Automated - UI & Functional Regression on iPad 2 running iOS 9\n\r' +
           'Automated - AU/NZ: iPad Air running iOS 8\n\r' +
           'Automated - DRM iPadAir running iOS 8\n\r';

var reg = /Automated - UI & Functional Regression on(.*)/;

var lines = test.split('\n\r')

console.log(lines);
var result = [];
for(var i = 0; i < lines.length; i++) {
  var regResult = reg.exec(lines[i]);
  if(regResult && regResult[1]) {
    console.log(lines[i], 'ok');
    result.push(regResult[1]);
  }
  else {
    console.log(lines[i], 'nok');
  }
}

console.log(result);

var regSecond = /(.*)running ([A-Za-z]*) ([0-9])/

var lstResult = []
for(var i = 0; i < result.length; i++) {
  var splitted = regSecond.exec(result[i]);
  console.log(splitted);
  lstResult.push({
    Phone: splitted[1].trim(),
    OS: splitted[2].trim(),
    'OS version': splitted[3].trim()
  })
}

console.log(lstResult);

Maybe regex must be more complex to get all case

3 Comments

There are few syntax errors such as not ending few declarations by ;. I assumed this is an example and I was looking at the plunker - maybe we can collaborate there. Plunker link has a non working code.
Let's say, I have test var as var test = "Automated - Under the Hood: iPhone 6 running iOS 9" only. For this example, I want to have output as Type: Under the Hood then on next line Phone: iPhone 6 then on next line OS type: iOS then on next line OS version: 9.
Nvm, I fixed all the things.
0

You can just split the text using test.split(' '); & store the result in an array. That way you will have everything separated & stored in its own var.

Comments

0

You can first use split on your string :

var x = dump.split(":");

This will give you an array of 2 items and the first 1 will be the type. Next you can take the second item of the array and split it on space. Then you can combine the new array that you get to make the new items . For example:

var phone = secondArray[0] + secondArray[1]; var os = secondArray[3];

and so on..

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.