简体   繁体   English

JavaScript字符串基于值拆分

[英]JavaScript String split based on value

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? 任何想法如何做到这一点?

You can use RegEx to search for matches. 您可以使用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 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) 

在此输入图像描述

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/ url(小提琴): https//jsfiddle.net/eugensunic/kphe5fbL/13/

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,')); 

You don't need Regex : 你不需要正则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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM