trueinViso
trueinViso

Reputation: 1394

How to match a specific regular expression?

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.

Upvotes: 0

Views: 77

Answers (2)

gosukiwi
gosukiwi

Reputation: 1575

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.

enter image description here

Example with Javascript

var r = /^\w{2}-(\d{2}-\w{3}-\d{2}).*?$/i
var groups = "CH-01-Feb-13-1".match(r);
console.log(groups);

Upvotes: 2

nicholasnet
nicholasnet

Reputation: 2277

If you are not comfortable with Regex then you can use something like this.

var str = 'CH-01-Feb-13-1';
str = str.replace('CH-','');
str = str.split('-');
str.pop();
console.log(str.join('-'));

Upvotes: 1

Related Questions