在字符串中添加文本和数字之间的空格

时间:2016-09-28 10:15:57

标签: jquery

如何在Jquery中的文本和数字之间添加空格。

我在变量中有字符串,例如:

var month = "June2016";

我希望它为"June 2016".

3 个答案:

答案 0 :(得分:1)

您可以使用在字母字符和数字字符之间插入空格的正则表达式。试试这个:

var month = "June2016".replace(/([a-z])(\d)/gi, '$1 $2');
console.log(month);

答案 1 :(得分:1)

只需匹配数字,然后在替换

中添加空格

var month = "June2016";
console.log(month.replace(/(\d+)/g, " $1"));

答案 2 :(得分:0)

使用match方法分割字符串。

var res = "june2016".match(/[a-zA-Z]+|[0-9]+/g);
console.log(res[0]); // will print month
console.log(res[1]); // will print year
var result = res[0]+" "+res[1];

结帐fiddle.