I am having a hard time understanding how to match a certain regular expression using javascripts match() function. I have a field in a table stored in the following format: CH-01-Feb-13-1. I want to be able to grab the date without the dashes, i.e. 01-Feb-13. I was trying to figure out how to combine with ^- or . but not sure how to do it.
我很难理解如何使用javascripts match()函数匹配某个正则表达式。我在表格中有一个字段,格式如下:CH-01-Feb-13-1。我希望能够在没有破折号的情况下获取日期,即01年2月1日。我试图找出如何与^ - 或。但不知道该怎么做。
2 个解决方案
#1
2
So you want the regular expression? Something like
所以你想要正则表达式?就像是
^\w{2}-(\d{2}-\w{3}-\d{2}).*?$
You can see the explanation here: http://www.regexper.com/ Just copy and paste the expression.
您可以在此处查看说明:http://www.regexper.com/只需复制并粘贴表达式即可。
Example with Javascript
使用Javascript的示例
var r = /^\w{2}-(\d{2}-\w{3}-\d{2}).*?$/i
var groups = "CH-01-Feb-13-1".match(r);
console.log(groups);
#2
1
If you are not comfortable with Regex then you can use something like this.
如果你对Regex不熟悉,那么你可以使用这样的东西。
var str = 'CH-01-Feb-13-1';
str = str.replace('CH-','');
str = str.split('-');
str.pop();
console.log(str.join('-'));
#1
2
So you want the regular expression? Something like
所以你想要正则表达式?就像是
^\w{2}-(\d{2}-\w{3}-\d{2}).*?$
You can see the explanation here: http://www.regexper.com/ Just copy and paste the expression.
您可以在此处查看说明:http://www.regexper.com/只需复制并粘贴表达式即可。
Example with Javascript
使用Javascript的示例
var r = /^\w{2}-(\d{2}-\w{3}-\d{2}).*?$/i
var groups = "CH-01-Feb-13-1".match(r);
console.log(groups);
#2
1
If you are not comfortable with Regex then you can use something like this.
如果你对Regex不熟悉,那么你可以使用这样的东西。
var str = 'CH-01-Feb-13-1';
str = str.replace('CH-','');
str = str.split('-');
str.pop();
console.log(str.join('-'));