0

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?

5
  • 3
    You could use \s+ to denote one or more white space characters. Commented Aug 11, 2015 at 6:15
  • 4
    What is the question? What do you want to extract/replace? Commented Aug 11, 2015 at 6:17
  • For Day, string re1 = "((?:Tues?|Thu(rs?)?|Sun|Mon|Wed|Fri|Sat))"; // Day Of Week 1 Commented Aug 11, 2015 at 6:41
  • You don't need different regex to match multiple spaces, re4 re6 re8 re10 re12 re14 can be replaced by re2 Commented Aug 11, 2015 at 6:44
  • Add complete code where you've used these regex patterns, so that the question will be clear Commented Aug 11, 2015 at 6:44

2 Answers 2

1

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.

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

2 Comments

You can also optimize the regex for day as you've done for month. To make some text optional
@Tushar: Do you mean precise the day regex? Like var reDay = "\\b((?:0?\\d|[12]\\d|3[01]))\\b"? I updated the answer.
1

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

3 Comments

I doubt whitespace trimming and shrinking is what OP needs.
The question does not state what OP needs except the part this answer answers :).
@AdamMoszczyński i changed question - it is ok

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.