senchaDev
senchaDev

Reputation: 587

JavaScript find if string has a substring that matches a given reg exp

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.

Upvotes: 1

Views: 59

Answers (2)

Vladimir Drenovski
Vladimir Drenovski

Reputation: 300

I have made an example with your regex that catches special chars which you can find here

As i said im using your regex by modifying it as such:

[AP]M[^a-zA-Z]-[^a-zA-Z]\w+[ ]?\w+[ ]?\w+:.

if you would like to also exclude digits you can modify it as such:

[AP]M[^a-zA-Z\d]-[^a-zA-Z\d]\w+[ ]?\w+[ ]?\w+:.

Also if you are expecting special characters in the name you can use \S instead of \w, this will include everything but white space chars. Then the regex will be as such:

[AP]M[^a-zA-Z]-[^a-zA-Z]\S+[ ]?\S+[ ]?\S+:.

i have updated the Regex101 example.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You may use /\b[AP]M\W+(?:-\W+)?([^:]+):/

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";
var ss = [ str1, str2, str3, str4 ]; // Test strings
var rx = /\b[AP]M\W+(?:-\W+)?([^:]+):/; 
for (var s = 0; s < ss.length; s++) {                  // Demo
  document.body.innerHTML += "Testing \"<i>" + ss[s] + "</i>\"... ";
  document.body.innerHTML += "Matched: <b>" + ((m = ss[s].match(rx)) ? m[1] : "NONE") + "</b><br/>";
}

Pattern details:

  • \b - a word boundary
  • [AP]M - AM or PM
  • \W+ - 1+ non-word chars
  • (?:-\W+)? - optional sequence of a hyphen and a non-word char
  • ([^:]+) - Group 1 (our output) capturing one or more chars other than :
  • : - a colon.

Since [^...] is a negated character class it will match any characters up to the first : (excluding that : from the match), but the trailing : in the pattern will actually require the presence of : in the string.

Upvotes: 1

Related Questions