0

I'm looking to target the following string using jQuery. Specifically, I need to wrap it in a strong tag for styling purposes. I can't change the source data. My regex-fu is pathetic. Any suggestions?

Nov 18, 2013, 4pm CST:

Thanks guys - these are excellent answers. I should have been slightly more specific - I need to match all occurrences of this format within a collection, e.g.:

$('.admin-comments').match(/[A-Z]{1}[a-z]{2}\s[0-9]{1,2},\s[0-9]{4},\s[0-9]{1,2}[a|p]m\s[A-Z]{3}/)

(I have a log of comments and I'm trying to wrap the timestamp in a strong element.)

Edit: Final Working Solution

  var adminComment = $('.admin-comments');

  if (adminComment.length) {
    var adminCommentTxt  = adminComment.text();
    var formatCommentTimestamp = adminCommentTxt.replace(/([A-Z]{1}[a-z]{2}\s[0-9]{1,2},\s[0-9]{4},\s[0-9\s]{1,2}[ap]m\s[A-Z]{3}\:)/g, "<strong>$1</strong>"); 

    adminComment.html(formatCommentTimestamp);    
  }
0

2 Answers 2

3

Here you go: /^[A-Z]{1}[a-z]{2}\s[0-9]{1,2},\s[0-9]{4},\s[0-9]{1,2}[a|p]m\s[A-Z]{3}\:$/

'Nov 18, 2013, 4pm CST'.match(/^[A-Z]{1}[a-z]{2}\s[0-9]{1,2},\s[0-9]{4},\s[0-9]{1,2}[a|p]m\s[A-Z]{3}\:$/)
["Nov 18, 2013, 4pm CST"]

Keep in mind that this regex is expecting the line to start and end with this date, if the date is contained within other text, remove the ^ from the start and the $ from the end.

Hope this helps.

To further explain the regex and hopefully ++ your "regex-fu"

  • [A-Z]{1} - match one upper case letter
  • [a-z]{2} - match two lower case letters

So far we are at Nov, Oct, Jan, etc.

  • \s - space
  • [0-9]{1,2} - a 1 (min) or 2 (max) digit number
  • , - literal comma
  • \s - space
  • [0-9]{4} - a 4 digit number (the year)

So now we have matched: Nov 18, 2013

  • , - literal comma
  • \s - space
  • [0-9]{1,2} - just like before, a one or two digit number
  • [a|p]m - 'a' or 'p' followed by an 'm'

Now we've matched: Nov 18, 2013, 4pm

  • [A-Z]{3} An upper case three character string
  • \: literal colon

That is the entire string.

Putting ^ at the beginning of the regex means the text we are matching against MUST begin with the pattern; similarly, the $ states that the text we are matching MUST end with the pattern.

Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Nice answer, +1. Just one catch, you missed the final ":" on the end of the OP's string.
Good catch, fixed to reflect the colon at the end of the string.
0

Rob M's answer is perfectly valid, and a more generic version than mine and may be exactly what you're looking for. However, if you want to be more specific with your months and time zones this may be useful for you:

(Oct)?(Nov)?\s\d{1,2},\s\d{4},\s\d{1,2}(pm)?(am)?\s(CST)?(PST)?:

This will match all of:

Nov 18, 2013, 4pm CST:
Oct 8, 2011, 11am PST:
Nov 02, 1981, 2am PST:
Oct 31, 1843, 12pm CST:

If you needed more months, you simply add each one like so:

(Mmm)? where Mmm corresponds to the month you want to match.

Similarly if you need more time zones, you'd add them like so:

(ZZZ)? where ZZZ corresponds to the timezone you want to match.

Similar to Rob's answer, if your date string is the only thing on the line, you could add the /^ and &/ prefix & suffix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.