The regular expression I am using is returning too much text. The expression is supposed to strip out a name and date from the text.
var sCurrentText = "(26 JAN 2011) - ILewis Provided excellent translation.";
var sRegxDate = "\([0-9]{2}[\\\.\/\-, ](Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\\\.\/\-, ][0-9]{4}\)[, \-]{0,3}[A-z]+ ?\-?\:? ?";
var DatePattern = new RegExp(sRegxDate, "i");
var sDate = DatePattern.exec(sCurrentText);
alert(sDate);
I expect:
(26 JAN 2011) - ILewis
But the popup says:
(26 JAN 2011) - ILewis,JAN
Other annoying issues:
- I am not able to emulate this issue in regexpal. http://regexpal.com/
2.
alert(sDate[0]);
returns:
(26 JAN 2011) - ILewis
but
oTextbox.value = sDate[0];
results in an empty textbox. Frustrating.
What am I doing wrong?
You are alerting
sDatethinking that it is a string, when in fact it is an array. The comma isn’t in the match, it is a separater.The first element is:
The second is:
This is because you have the month bit wrapped in parentheses. It is a capturing group and
execreturns capturing groups as part of the result.You want
If you are getting an empty textbox from this, there is some other issue here outside of the regex.
You also have some improperly escaped
\in your regex. As it is there, it does not work at all in IE. You need\\wherever you have a\EDIT: This fiddle shows the code in action. It works.
http://jsfiddle.net/5azr9/