简体   繁体   English

JS:字符串的正则表达式

[英]JS: Regular expression for string

i have js and a different strings like this: 我有js和像这样的不同字符串:

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 单词betweenand没有改变,但有时我能得到这个字符串不同量的单词之间有空格

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? 如何简化此行的正则表达式?

try this: 尝试这个:

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

it will remove all your extra space and keep just 1 space each time. 它将删除您所有的多余空间,每次仅保留1个空间。 so if you have 3 spaces like you said it will convert it to 1 space 因此,如果您有3个空格,如您所说,它将转换为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? 请注意,我没有捕获空格(删除了括号),添加了一个reDay几天,仅用\\b\\d{1,2}\\b捕获了整个单词的两位数,并且我已经获取了一些正则表达式(已删除)并通过更改\\s?固定时间正则表达式\\s? to \\s* . \\s* It looks like that was the main problem since ? 看来这是主要问题,因为? stands for 0 or 1 occurrence , and * means 0 or more occurrences . 代表0或1次出现*表示0次或多次出现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM