javascript中的正则表达式是否要删除字符串中的左零?

javascript中的正则表达式是否要删除字符串中的左零?,javascript,regex,Javascript,Regex,如何在javascript中创建正则表达式来删除字符串中的左零 我有这个: 2015年1月1日 我需要得到这个: 2015年1月1日您可以尝试不使用正则表达式: var myDate="01/01/2015"; var d = new Date(myDate); alert(d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear()); 如果这是一个日期,那么Zan的方法可能是更好的方法。但是,如果您真的想使用正则表达式,那么这里有

如何在javascript中创建正则表达式来删除字符串中的左零

我有这个:

2015年1月1日

我需要得到这个:


2015年1月1日

您可以尝试不使用正则表达式:

var myDate="01/01/2015";
var d = new Date(myDate);
alert(d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear());

如果这是一个日期,那么Zan的方法可能是更好的方法。但是,如果您真的想使用正则表达式,那么这里有一种方法:

要仅删除第一个前导零,请执行以下操作:

"01/01/2015".replace(/^0(.*)/,"$1")
更详细的:

str = "01/01/2015"
pat = /^0(.*)/      // Match string beginning with ^, then a 0, then any characters.   
str.replace(pat,"$1")    // Replace with just the characters after the zero
要删除每个分组中的前导零,请执行以下操作:

str = "01/01/2015"
pat = /(^|[/])0(\d*)/g  //  Match string begin ^ or /, then a 0, then digits. 'g' means globally. 
str.replace(pat,"$1$2")  // Replace with the part before and after the 0.

希望这就是你想要的:

s = '01/01/2015'; // check  11/01/2015 、11/11/2015、01/10/2015 ...
s = s.replace(/0*(\d+)\/0*(\d+)\/(\d+)/,"$1/$2/$3");
alert(s);

正则表达式很好,但更详细的是,并不是每个月都删除零。请添加一个解释,说明为什么这会解决OP的问题。帮助别人理解。