[英]JavaScript regex whitespace
Running the following JavaScript code finds, for example, "12 December" successfully. 运行以下JavaScript代码,例如,成功找到“ 12 December”。
return messageHtmlBody.match(/[1-31]{1,2}(\s)[a-zA-Z]{3,9}/i)[0];
I would like to return "12 December 2012" so tried this code: 我想返回“ 2012年12月12日”,因此尝试了以下代码:
return messageHtmlBody.match(/[1-31]{1,2}(\s)[a-zA-Z]{3,9}(\s)\d{4}/i)[0];
Not only did this not return the match, but the code didn't even run successfully. 这不仅没有返回匹配,而且代码甚至没有成功运行。 I tried the following too (just the second (\\s) character) and that didn't run either:
我也尝试了以下操作(仅第二个(\\ s)字符),但均未运行:
return messageHtmlBody.match(/[1-31]{1,2}(\s)[a-zA-Z]{3,9}(\s)/i)[0];
Is there a reason why the second (\\s) wouldn't work? 第二个(\\ s)不起作用是有原因的吗? The first (\\s) matches the first white space successfully.
第一个(\\ s)成功匹配第一个空格。 The search string 100% contains the string "12 December 2012" so finding it should not be the issue.
搜索字符串100%包含字符串“ 2012年12月12日”,因此找到它应该不是问题。
Any ideas? 有任何想法吗?
[1-31]
is not the valid regex for "a number between 1 and 31". [1-31]
不是“ 1到31之间的数字”的有效正则表达式。 All it does is accept any of 1, 2, 3 and (with the quantifier) any of 11, 12, 13, 21, 22, 23, 31, 32, 33. 它所做的就是接受1,2,3中的任何一个,并且(带量词)接受11,12,13,21,22,23,31,32,33中的任何一个。
Instead, it should be (?:3[01]|[1-2][0-9]|[0-9])
相反,它应该是
(?:3[01]|[1-2][0-9]|[0-9])
Also, it is unnessecary to put parentheses around the \\s
. 同样,在
\\s
周围加上括号也是不必要\\s
。
To be more specific, you could also explicity state what months are with: 更具体地讲,您还可以明确说明使用什么月份:
(?:(?:jan|febr)uary|march|april|may|june|july|august|(?:(?:sept|nov|dec)em|octo)ber)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.