Regular expression including spaces and newlines
If I wanted to do a split on 开发者_高级运维some text between "/div>"
plus (either a new line or a space) plus "<div id="
how would i do this using regular expressions in JavaScript? I thought it would be:
"/div>" + "[' ']*[\r\n]" + "<div id"
Your regular expression looks strangely like string concatenation.
Also, [' ']*[\r\n]
matches zero or more spaces or '
in between, followed by either \r
or \n
. What you really want is either a space, \r
, or \n
, which can be expressed using a character class (matches any single character within the [
]
brackets):
[ \r\n]
In general, for matching whitespace (which includes tabs, newlines, and spaces), you can just use the special \s
pre-defined character class instead of defining your own.
Example:
var str = "/div> <div id";
var parts = str.split(/\s/, 1); // [ '/div>', '<div id' ]
精彩评论