JS:字符串的正则表达式

时间:2015-08-11 06:13:35

标签: javascript regex date

我有js和这样的不同字符串:

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

单词betweenand未更改 但有时我可以在单词之间使用不同数量的空格来获取此字符串

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

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

是否可以为此字符串创建正则表达式?

  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

如何简化此行的正则表达式?

2 个答案:

答案 0 :(得分:1)

试试这个:

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

它将删除所有额外空间,每次只留一个空格。 所以,如果你有3个空格,就像你说它将它转换为1个空格

答案 1 :(得分:1)

以下是我尝试重复使用您的代码:



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]);
  
}
&#13;
&#13;
&#13;

请注意,我没有捕获空格(已移除的括号),为reDay添加了\b\d{1,2}\b天,只用\s?捕获两个数字作为整个单词,并且我已经倾斜了一些正则表达式(删除了不必要的括号)并通过将\s*更改为?来修复时间正则表达式。看起来这是主要问题,因为*代表 0或1次出现set.seed(1) #Create the clusters library(doParallel) cl <- makeCluster(detectCores()) registerDoParallel(cl) #Export the environment variables to each cluster clusterExport(cl,ls()) #Load the library "rgeos" to each cluster clusterEvalQ(cl, library(rgeos)) #Split the data ID.Split<-clusterSplit(cl,unique(poly1$ID)) #Define a function that calculates the distance of one ID in relation to the poly2 a<-function(x) gDistance(spgeom1 = poly1[x,], spgeom2 = poly2, byid=TRUE) #Run the function in each cluster system.time(m<-clusterApply(cl, x=ID.Split, fun=a)) #Cluster close stopCluster(cl) #Merge the results output<- do.call("cbind", m) 表示 0或更多次出现