开发者

Regular expression including spaces and newlines

开发者 https://www.devze.com 2023-02-17 04:09 出处:网络
If I wanted to do a split on 开发者_高级运维some text between \"/div>\" plus (either a new line or a space) plus \"<div id=\"

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' ]
0

精彩评论

暂无评论...
验证码 换一张
取 消