var str = "4/16/14, 10:24 AM - John Doe: How is everything going on? Check this: iPhone7!" I want to check if string contains a substring that matches AM - <some-name>:. For eg, in above string it should match AM - John Doe: and return John Doe. (of course once it matches, I can get the name using substring). Also, Sometimes there maybe special characters instead of white spaces in AM - John Doe:. Regular expression should work in this case also.
eg:
var str1 = "4/16/14, 10:24 AM - John Doe likes your photo"; var str2 = "4/16/14, 10:24 AM John Doe replied to your comment"; var str3 = "4/16/14, 10:24 AM John Doe: Whats going on"; var str4 = "4/16/14, 10:24 AM John Doe: Whats going on : hmmm"; The regular expression should match str3 and str4 since it contains a sub-string that begins with AM and ends with the first :
For both str3 and str4, I want to get the name John Doe. Note: str1 and str2 has John Doe too but there it does not immediately trail by :
Expressions I have tried:
str.match(/[AP]M - \w+[ ]?\w+[ ]?\w+:./); Above fails when there are special characters such as UTF-8 characters. It is not visible but there seems to be characters such as e2 80 80.
U+2000, EN QUAD. Acc. to MDN,\smatches[ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]. I think/\b[AP]M\s+(?:-\s+)?([^:]+):/should work for you. If not, try the one I currently have in the answer.