JavaScript分割功能 [英] JavaScript split function

查看:134
本文介绍了JavaScript分割功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据,字符使用JavaScript分割字符串

i like to split a string depending on "," character using JavaScript

示例

var mystring="1=name1,2=name2,3=name3";

需要这样输出

1=name1
2=name2
3=name3


推荐答案

var list = mystring.split(',');

现在你有一个的数组['1 = name1','2 = name2','3 = name3']

如果您想要输出全部用空格分隔,您可以这样做:

If you then want to output it all separated by spaces you can do:

var spaces = list.join("\n");

当然,如果这真的是最终目标,你也可以用空格替换逗号:

Of course, if that's really the ultimate goal, you could also just replace commas with spaces:

var spaces = mystring.replace(/,/g, "\n");

(编辑:您的原始帖子在代码块中没有您想要的输出,所以我想你是在追求空间。幸运的是,相同的技术可以获得多行。)

( Your original post didn't have your intended output in a code block, so I thought you were after spaces. Fortunately, the same techniques work to get multiple lines.)

这篇关于JavaScript分割功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆