4

I have a single variable object value in this format:

"Landing Hits: 0Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 0Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0"

I need to convert it into:

"Landing Hits: 0, Rewards Hits: 0, Facebook Posts: 0, Twitter Tweets: 0, Twitter Autofollows: 0, Instagram Photos: 0, Instagram Likes: 0, Instagram Votes: 0, Pinterest Pins: 0, Form Submissions: 0, Submissions: 0, Engagement: 0, Views: 0, Prints: 0"

Any ideas how to do this?

1
  • seems like you should fix the source Commented Aug 8, 2015 at 12:10

4 Answers 4

4

You can use RegEx to search for matches.

var str = "Landing Hits: 0Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 0Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0";

var arr = str.match(/[^:]+\s*:\s*\d+/g); //Find values like `String:Number`

var out = arr.join(", "); //Convert array matches to String

document.write("<pre>" + out + "</pre>"); //Test

Just replace: If you'll only need is to replace the string, you could also do this:

var str = "Landing Hits: 0Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 0Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0";

var formatted = str.replace(/(\d)(\w)/g, "$1, $2");

document.write(formatted)

Extending into a Object: This allows you to work the values obtained for another purpose

var str= "Landing Hits: 0Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 0Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0"
, regex = /([^:]+)\s*:\s*(\d+)/g, collection = {}, temp;

while(temp = regex.exec(str)){
  collection[temp[1]] = +temp[2]
}

console.log(collection)

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help. very nice solution. In Csv file the key and value is coming in same column, now I want to display value in other column relevant to key......any suggestion ??
You could provide an example in your question?
I have created csv download functionality. So, when I download in excel file, The key and value is coming in same column . So, just was trying to get value in just second column relevant to key.
0

add the following line in your code

var str = "Landing Hits: 0Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 0Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0";

alert(str.replace(/0|_/g,'0,'));

1 Comment

The value will not always be zero
0

You don't need Regex:

var str = "Landing Hits: 0Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 0Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0";
var newStr = str.split(0).join('0, ').slice(0, -2);

Comments

0

You could try this:

var c = "Landing Hits: 0345Rewards Hits: 0Facebook Posts: 0Twitter Tweets: 034534532Twitter Autofollows: 0Instagram Photos: 0Instagram Likes: 0Instagram Votes: 0Pinterest Pins: 0Form Submissions: 0Submissions: 0Engagement: 0Views: 0Prints: 0";
var array = [];
var counter = 0;
var num;
var dup = '';
for (var i = 0; i < c.length; i++) {
  dup += c[i];
  if (c[i] === ':') {
    dup += ' ';
  }
  if (!isNaN(c[i]) && isNaN(c[i + 1]) && c[i] !== ' ') {
    dup += ', ';
  }
}
alert(dup);

url (fiddle):https://jsfiddle.net/eugensunic/kphe5fbL/13/

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.