Lola
Lola

Reputation: 2648

JS: Regular expression for string

i have js and a different strings like this:

Tue Aug 11 2015 between  4:00 PM and  5:00 PM

words between and and not changed But sometimes i can get this string with different amount of spaces between words

Tue Aug 11 2015 between   4:00 PM and   5:00 PM  (3 spaces)

or

Tue Aug 11 2015 between 4:00 PM and 5:00 PM (1 spaces)

Is it possible to create a regular expression for this string?

  string re1="((?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|Tues|Thur|Thurs|Sun|Mon|Tue|Wed|Thu|Fri|Sat))";  // Day Of Week 1
  string re2="(\\s+)";  // White Space 1
  string re3="((?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?))"; // Month 1
  string re4="(\\s+)";  // White Space 2
  string re5="((?:(?:[0-2]?\\d{1})|(?:[3][01]{1})))(?![\\d])";  // Day 1
  string re6="(\\s+)";  // White Space 3
  string re7="((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])";    // Year 1
  string re8="(\\s+)";  // White Space 4
  string re9="(\"between\")";   // Double Quote String 1
  string re10="(\\s+)"; // White Space 5
  string re11="((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)"; // HourMinuteSec 1
  string re12="(\\s+)"; // White Space 6
  string re13="(\"and\")";  // Double Quote String 2
  string re14="(\\s+)"; // White Space 7
  string re15="((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)"; // HourMinuteSec 2

how to simplify the regular expression for this line?

Upvotes: 0

Views: 131

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

Here is my attempt to re-use your code:

var re1="((?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|Tues|Thur|Thurs|Sun|Mon|Tue|Wed|Thu|Fri|Sat))";  // Day Of Week 1
var re2="\\s+";  // White Space 1
var re3="((?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?))"; // Month 1
var re5="((?:(?:[0-2]?\\d)|(?:3[01])))(?!\\d)";  // Day 1
var re7="(\\b(?:1\\d{3}|2\\d{3})\\b)";    // Year 1
var re11="((?:[0-1][0-9]|2[0-3]|[0-9]):[0-5][0-9](?::[0-5][0-9])?(?:\\s*(?:am|AM|pm|PM))?)"; // HourMinuteSec 
var reDay = "\\b((?:0?\\d|[12]\\d|3[01]))\\b";

var s = "Tue Aug 11 2015 between  4:00 PM and  5:00 PM";
var rx = RegExp(re1 + re2 + re3 + re2 + reDay + re2 + re7 + re2 + "between" + re2 + re11 + re2 + "and" + re2 + re11, 'i');
if ((m = rx.exec(s)) !== null) {
  document.write("Day of week: " + m[1] + "<br/>");
  document.write("Month: " + m[2] + "<br/>");
  document.write("Day: " + m[3] + "<br/>");
  document.write("Year: " + m[4] + "<br/>");
  document.write("From: " + m[5] + "<br/>");
  document.write("Till: " + m[6]);
  
}

Note that I am not capturing whitespace (removed parentheses), added a reDay for days that just captures two digits as a whole word with \b\d{1,2}\b, and I have leaned out some of your regexps (removed unnecessary brackets) and fixed the time regex by changing \s? to \s*. It looks like that was the main problem since ? stands for 0 or 1 occurrence, and * means 0 or more occurrences.

Upvotes: 1

Dkova
Dkova

Reputation: 1097

try this:

string.replace(/\s+/g,' ').trim();

it will remove all your extra space and keep just 1 space each time. so if you have 3 spaces like you said it will convert it to 1 space

Upvotes: 1

Related Questions