javascript 如何在javascript中拆分函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6453840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:43:54  来源:igfitidea点击:

how to split function in javascript

javascript

提问by Saloni

i have data in

我有数据

var description="Name:John;EmployeeID:2;Salary:00;Address:London";

i want the result as

我想要结果为

Name: John
Employee Id: 2
Salary: 00
Address: London

is it possible with split() function in javascript?

javascript 中可以使用 split() 函数吗?

采纳答案by JMax

With this statement:

有了这个声明:

var arrDescription = description.split(";");

you will get an array with all the values. For more info on splitcheck the following link.

你会得到一个包含所有值的数组。有关split检查以下链接的更多信息。

you can even join them afterwards :

你甚至可以事后加入他们:

printf(arrDescription.join(" "));

For more info on joincheck the following link.

有关join检查以下链接的更多信息。

Max

最大限度

回答by Matt Ball

You can do it with String.split()but in this case it's simpler to use String.replace():

你可以这样做,String.split()但在这种情况下使用更简单String.replace()

var description="Name:John;EmployeeID:2;Salary:00;Address:London";
description = description.replace(/;/g, '\n').replace(/:/g, ': ');
/*
"Name: John
EmployeeID: 2
Salary: 00
Address: London"
*/

回答by Bergius

If you want the result as an object, try:

如果您希望结果作为对象,请尝试:

var f = function (str) {
    var x = {}, key2label = { EmployeeID: 'Employee Id' };
    str.replace(/(.+?):(.+?)(;|$)/g, function (match, key, value) {
        key = key2label[key] || key;
        x[key] = value;
    });
    return x;
};

If a simple string is needed, but you still need to replace keys:

如果需要一个简单的字符串,但您仍然需要替换键:

var f2 = function (str) {
    var key2label = { EmployeeID: 'Employee Id' };
    return str.replace(/(.+?):(.+?)(;|$)/g, function (match, key, value, semi) {
        key = key2label[key] || key;
        return key + ': ' + value + (semi ? '\n' : '');
    });
};

If you really didn't mean to replace keys, this will do it:

如果您真的不想更换密钥,则可以这样做:

var f3 = function (str) {
    return str.split(':').join(': ').split(';').join('\n');
};

... or use Matt Ball's answer.

...或使用马特鲍尔的答案。

回答by Srikanth Venkatesh

You can probable try like this to display.

你可以试试这样显示。

       var description="Name:John;EmployeeID:2;Salary:00;Address:London";
       var arr=new Array();
       arr=description.split(";"); 
       for(var i=0;i<arr.length;i++)
           document.writeln("<h4>"+arr[i]+"</h4>");    

回答by Daniel A. White

Yes.

是的。

You first should spliton the semicolon ;. Loop through those results, and spliteach result on each colon :.

你首先应该split在分号上;。循环遍历这些结果,以及split每个冒号上的每个结果:

You will have to build the result by hand.

您将不得不手动构建结果。

回答by ajit Paswan

var description="Name:John;EmployeeID:2;Salary:$8000;Address:London"; var splitted = description.split(";");

var description="Name:John;EmployeeID:2;Salary:$8000;Address:London"; var splitted = description.split(";");

for(var i = 0; i < splitted.length; i++) { document.write(splitted[i] + ""); }

for(var i = 0; i < splitted.length; i++) { document.write(splitted[i] + ""); }