-4

I have a string Born (1970-07-30) 30 July 1970 (age 43) London, UK

I need to get the sub string 30 July 1970 from this. Like wise for any month, ie. the DOB may only change .

Can any one please help me with a regular expression for this ?

The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
Ashwin
  • 455
  • 3
  • 7
  • 15

3 Answers3

4
var string = "Born (1970-07-30) 30 July 1970 (age 43) London, UK";

var pattern = /\s(\d{1,2}\s[a-zA-Z]+\s\d{4})\s/

//fetch all matches back into an array
var result = string.match(pattern);

alert(result[1]);

Example on jsFiddle

Regular expression visualization

Edit live on Debuggex

Hope this helps.

Community
  • 1
  • 1
Jake Aitchison
  • 1,079
  • 6
  • 20
2

I wouldn't bother with regex, there is a common enough pattern to just pull it apart...

var string = "Born (1970-07-30) 30 July 1970 (age 43) London, UK";
string = string.substring(string.indexOf(")") + 1);//" 30 July 1970 (age 43) London, UK"
string = string.substring(0, string.indexOf("("));//" 30 July 1970 "
string = string.trim();//"30 July 1970"
//string = "30 July 1970"
musefan
  • 47,875
  • 21
  • 135
  • 185
  • there is a potential here that the number of spaces might change after the `(` and or before the `)`. "potentially" anyway :D – Jake Aitchison Aug 14 '13 at 13:27
1

If they're always formatted like this you don't need a regular expression. You can use split and slice.

var textDate = yourString.split(' ').slice(2, 5).join(' ');
Bill Criswell
  • 32,161
  • 7
  • 75
  • 66